There are lots of good resources on how to alter the main query, or add secondary loops...
But I actually want to completely replace the main query with a different query altogether.
I am hijacking one of my pages to serve, essentially, as a category page. (Note I am changing from "page" to "post" in my effort to do this.) But when I alter the main query by doing this:
function front_page_announcements( $query ) {
if ( $query->query_vars['page_id'] == 84 && $query->is_main_query() && !$query->is_admin ) {
$query->set("category_name", "special-announcement");
$query->set("page_id", NULL);
}
}
add_action( 'pre_get_posts', 'front_page_announcements' );
I end up getting every page (not post) in the database. What I actually want is every post (not page) with a category id of 3
, or special-announcement
in my case.
I don't want to make unnecessary calls to the database, so it seems like the pre_get_posts
action hook is what I'll want, but I can't figure it out. Even just setting $query = new WP_Query('cat=3')
doesn't seem to work.