0

I am working on a site where I use get_template_part to retrieve the loop. In the header of the template I specified so it runs the global query.

global $user_ID, $wp_query, $pagename;

Now, what does best practice say if I want multiple queries on the same page? I tried to simply go in and specify another global variable e.g. $wp_query2 in the header, but I somewhere read that using global variables for this is actually bad practice.

Unfortunately, I cannot rely on slugs for the loop or I would use that. It's the frontpage. I was thinking about passing a variable via "get_template_part" but that is not supported?

Never had to do this, so what would you use?

1

You shouldn't have to globalize the main $wp_query, only secondary queries.

Take the following example, if I have a very simple frontpage.php template it would look like:

<?php get_header(); ?>

    <?php get_template_part( 'templates/template', 'main-query' ); ?>

<?php get_footer(); ?>

I can then create a template found in /theme-name/templates/template-main-query.php which has a normal Loop:

<?php if( have_posts() ) : ?>

    <?php while( have_posts() ) : the_post(); ?>

        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>

    <?php endwhile; ?>

<?php endif; ?>

Note, I don't need any kind of global query unless I want to access any of the query methods / properties like found_posts or something of the like.


For secondary queries you could globalize the query but a better solution would be to include the template like so:

<?php get_header(); ?>

    <?php 
        get_template_part( 'templates/template', 'main-query' );

        $all_pages = new WP_Query( array(
            'post_type'     => 'page',
            'posts_per_page'=> -1
        ) );

        require_once( locate_template( 'templates/template-secondary-query.php' ) );
    ?>

<?php get_footer(); ?>

Then our template file would like like this:

<?php if( $all_pages->have_posts() ) : ?>

    <?php while( $all_pages->have_posts() ) : $all_pages->the_post(); ?>

        <h1><?php the_title(); ?></h1>

    <?php endwhile; ?>

<?php endif; ?>

If at all possible, it would be both cleaner and easier to just include the secondary new WP_Query in the actual template file versus having your query in one file and loop in another. If I'm not mistaken, this is the method WooCommerce uses for their secondary queries in template files, they just include the new query at the top of the file.

1

This is how you can make additional queries in a template. Just strip whatever argument sections you don't need.

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array( 'post_type'=>'post', 'posts_per_page'=>-1, 'paged'=>$paged );
$args = array_merge( $args, array( 'orderby'=>'menu_order', 'order'=>'ASC' ) );
$args['tax_query'] = array(
    array(
        'taxonomy' => 'tax_slug',
        'terms' => $term_id,
        'field' => 'id',
    ),
);
$args['meta_query'] = array(
    array(
        'key' => 'meta_key',
        'value' => $value,
        //'compare' => 'IN',
    ),
);
$data = new WP_Query($args);
while( $data->have_posts() ) {
  $data->the_post();
  // show data from the post
}
wp_reset_query();

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.