First thing, English is not my native language, and I am pretty new to programming, PHP and WordPress, so please be patient and very simple in your answers :)
I am creating a custom theme for a multi-feature blog displaying videos, articles, movie reviews, image galleries, events and a shopping area where visitors can buy and download some e-books.
In the front page I want to show a sort of general summary of last entries, including:
My question is: do I need to call 7 different WP_Query objects to have it working, one for each category/post_type, or are there better ways to get it done?
Maybe using the pre_get_posts hook, or some conditional tags like get_post_type() or is_category()? I tried those and some other ways but my lack of programming skills got me nowhere. Any suggestion is very welcome.
(added the relevant parts of code I am actually using)
<!-- // start Front Page contents - BLOG-->
<!-- start tab dei post -->
<!-- // Define our WP Query Parameters-->
<?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'post' ) ); ?>
<!--// seleziona risultati per categoria - POST -->
// here is included the Loop for - POST
<?php wp_reset_postdata(); ?>
<!-- end tab dei post -->
<!-- start tab rece -->
<!-- // Define our WP Query Parameters-->
<?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'rece' ) ); ?>
<!--// seleziona risultati per categoria - RECE -->
// here is included the Loop for - REVIEWS
<?php wp_reset_postdata(); ?>
<!-- end tab rece -->
<!-- start tab video -->
<!-- // Define our WP Query Parameters-->
<?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'vide' ) ); ?>
<!--// seleziona risultati per categoria - VIDE -->
// here is included the Loop for - VIDEOS
<?php wp_reset_postdata(); ?>
<!-- end tab video -->
<!-- // end Front Page contents - BLOG-->
<!-- // start Front Page contents - GALL-->
<!-- // Define our WP Query Parameters-->
<?php $the_query = new WP_Query( array( 'posts_per_page' => '3','category_name' => 'gall' ) ); ?>
<!--// Start our WP Query-->
// here is included the Loop for - GALLERIES
<?php wp_reset_postdata(); ?>
<!-- // end Front Page contents - GALL-->
<!-- // start Front Page contents - BOOK-->
<!-- // Define our WP Query Parameters-->
<?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'download' ) ); ?>
<!--// seleziona risultati per categoria - BOOK -->
// here is included the Loop for - DOWNLOADS
<?php wp_reset_postdata(); ?>
<!-- // end Front Page contents - BOOK-->
<!-- // start Front Page contents - EVEN-->
<!-- start tab EVENTI FUTURI -->
<!-- // Define our WP Query Parameters-->
<?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'event','scope' => 'future' ) ); ?>
<!--// Start our WP Query-->
// here is included the Loop for - Incoming Events
<?php wp_reset_postdata(); ?>
<!-- end tab EVENTI FUTURI -->
<!-- start tab EVENTI PASSATI -->
<!-- // Define our WP Query Parameters-->
<?php $the_query = new WP_Query( array( 'posts_per_page' => '3','post_type' => 'event','scope' => 'past' ) ); ?>
<!--// Start our WP Query-->
// here is included the Loop for - Past Events
<?php wp_reset_postdata(); ?>
<!-- end tab EVENTI PASSATI -->
<!-- // end Front Page contents - EVEN-->
Yes, you have to query everything on separate ways if they are in different post types.
In the blog categories case you can do something like:
$pp = 3;
$cats = ("post","rece","gall",...);
foreach($cats as $cat){
$the_query = new WP_Query( array( 'posts_per_page' => $pp,'category_name' => $cat ) );
//Loop in here
}
This way you can save lines and time, besides you can apply same logic on post type queries if they are the same post type.
Let me know if this works for you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With