- I created a Custom Post Type (call it "my-post-type").
- I created an
archive-my-post-type.php
page for displaying all the Custom Post Types. - Looking at the "Recent Posts" section of the Sidebar, I noticed none of custom post types are showing up.
- I'd like the Custom Post Types to show in Recent Posts as well.
- I read this article here on WP Stack which says I can use
pre_get_posts
to add my Custom Post Type to the Recent Posts (sidebar). - Tried it. That did not work.
So...
I did some investigation on pre_get_posts
and from what I can tell...
pre_get_posts
is used to alter the main loop.
Also the codex gives this warning:
Identifying Target Queries
When using pre_get_posts, be aware of the query you are changing. One useful tool is is_main_query(), which can help you ensure that the query you are modifying is only the main query.
- That phrase "target queries" in the title seems (to me) to imply that I can "target" which query I want to modify.
- Also the code from stack also does not use
is_main_query()
. - So now I'm wondering... are "Recent Posts" NOT part of the "main query"?
So I added some code to see exactly what queries were run on the page and saw two queries...
This query was above the entire page (followed by all my Custom Posts)...
'SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'press-release' AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 10'
The other query was above the sidebar...
'SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 5'
I'd like to add my Custom Post Type to this query.
CAN (and SHOULD) I accomplish this using pre_get_posts?
I'm pretty green, so I really hope this question makes sense.