• Resolved befranga

    (@befranga)


    I would like to make a page template with a custom query where I want to query mutliple categories as one query.

    This is my code `
    <?php
    $paged = ( get_query_var(‘paged’) ) ? get_query_var(‘paged’) : 1;

    $custom_args = array(
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => 5,
    ‘category__and’ => array( 193,192,191 ) ,
    ‘paged’ => $paged
    );

    $custom_query = new WP_Query( $custom_args ); ?>

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

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

    <?php get_template_part( ‘content’, ‘pagecat’ ); ?>

    <?php endwhile; // end of the loop. ?>
    <?php
    if (function_exists(custom_pagination)) {
    custom_pagination($custom_query->max_num_pages,””,$paged);
    }
    ?>

    <?php wp_reset_postdata(); ?>

    <?php else: ?>
    <p><?php _e( ‘Sorry, no posts matched your criteria.’ ); ?></p>
    <?php endif; ?>

    `

    It’s not working though. The categories are subcategories. Does this not work with subcategories? I thought as they have a unique ID..

    Thanks for any help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    When you say that it’s not working, what precisely do you mean? Are you seeing the ‘Sorry, no posts matched your criteria.’ message instead of posts, or something else?

    Your custom query is using category__and so it will only display posts that are in all three of the categories you’ve specified. If you want to display posts that are in any of the three categories, use category__in instead.

    Thanks,
    Lucas

    Thread Starter befranga

    (@befranga)

    That was exactly the problem. Thanks for your answer!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Page with custom query’ is closed to new replies.