5
votes

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:

  • last 3 entries for each of 3 different blog-post categories (articles, movie reviews, videos);
  • next 3 incoming events
  • last 3 past events
  • last 3 published galleries
  • last 3 downloadable e-books

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-->
1
If your plan is to display all of these entries on separate sections of your homepage (and it seems to be the case according to your code) then what you're doing is fine IMO.cabrerahector
Thanks for the reply. Yes that's the plan. My concern was about the method because I checked some other themes and they usually show at most one instance of wp_query in a single page. I also installed Query Monitor and it detects 94 total DB queries in my front-page, while I read around that about 100 queries in a single WP page are a bit too much. Then again, I really am new to programming and WP so I thought to ask for opinions in here.Krnell
As long as you use some sort of caching mechanism on your site to avoid running potentially-heavy database queries too frequently you shouldn't have any issues (eg. WP Super Cache to generate static HTML files from your website's dynamic pages, or more advanced stuff like Redis/Memcached to cache most of your site's data on a persistent object cache, etc.) But it's nice too see you worrying about performance, that's what good developers do :)cabrerahector
Great question by the way!Krys

1 Answers

1
votes

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!