I was tasked to display posts group by their categories, and I suppose I could do that with WP_Query
. The problem is, wordpress still queries for the latests posts on the index page. How do I prevent that from happening?
I used the plugin MySQL Profiler to check what were being queried.
Why don't you just use custom query in index.php.
Something like this:
$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'posts_per_page'=>5,
'orderby' => 'date',
'order' => 'DESC'
);
$temp=$wp_query;//save the main query
$wp_query=new WP_Query($args);
while ( have_posts() ) : the_post();
get_template_part(...);
endwhile;
$wp_query=$temp;//restore the main $wp_query
And to group posts by categories I would use this code from WordPress StackExchange.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.