• I created a plugin for a custom post type and am pulling the posts in via wp_query, however I’m wondering if the way I’m handling this is the best option. It’s working as expected so I won’t get into the details but the general file setup to handle this.

    1). I created the plugin then set up an includes directory in my custom theme which contains a queries.php file
    2). Then Added a require get_template_directory() . '/inc/queries.php'; in my functions file
    3). Then in my queries.php I created a function to handle the WP_Query and run the loop and display my content.
    4). I wanted this on page-work.php so in page-work I make a function call to the function I set up in queries.php and now all those custom posts are being displayed in page-work as expected.

    For example: If I were going to make 3 more custom post types – would I put them into the existing plugin and then create a new function for each post type in the queries file….then just call each function where I need them?

    Or could I eliminate the queries file and just write each function directly in the file I need them: so for page-work eliminate the function call and just write the function directly in that file?

    I tried to lay this out as clear as possible as I have been wondering about this for a couple days. This is a custom theme dev project for my personal site.

    I appreciate any suggestions, Thank you

    • This topic was modified 4 years, 3 months ago by Jan Dembowski.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You really should keep all the handling of the custom post types in the same place. The theme doesn’t need a custom query. WordPress will query the correct thing for each post type. All the plugin needs to do is filter. Either wholly replace the template file by filtering `template_include’
    https://developer.wordpress.org/reference/hooks/template_include/
    or provide template parts for the content and filter them in with ‘template_include’ or just add on to the end of existing content by filtering ‘the_content’.

    You can make your plugin for custom post type a mu-plugin, so that it is always active. Try to keep as much of it out of the theme as possible, so you can still switch themes easily. If you want to put templates in your theme instead, put them in a child theme (you can name them with the custom post type, so they match the Template Hierarchy), and then all the custom post stuff is together in the child theme, where it’s easier to switch to a different theme.

    Be sure you really need custom post types. Perhaps all you really need is taxonomies.

    Thread Starter tkmets5

    (@tkmets5)

    I think I was trying to do more than was necessary. I ended up just creating a plugin for the new post type then a custom post type archive file to display the posts how I wanted them. As I learn more about theme development I’m understanding how to let WordPress work for me more often. Thank you.

    • This reply was modified 4 years, 3 months ago by tkmets5.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom queries using wp_query for custom post types’ is closed to new replies.