0

I'm having trouble trying to create a second query on the category.php pages, what I'm trying to achieve is displaying the most popular posts from the current category archive page being viewed.

This is what I have so far.

<?php // The Query
    global $wp_query;

    $term = $wp_query->queried_object;

    $args=array(
        'meta_key' => 'views',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 5,
        'post_type' => 'post',
        'tax_query' => array(
                array(
                    'taxonomy'  => $term->taxonomy,
                    'field'     => 'slug',
                    'terms'     => $term->slug,
                    )
                )
        );

    $new_query = null;
    $new_query = new WP_Query($args);

    while ($new_query->have_posts()) : $new_query->the_post(); ?>

    <?php include(TEMPLATEPATH.'/category-slider.php'); ?>

<?php
    endwhile;
    wp_reset_postdata(); ?>

Now for the most part it works but the only problem I'm having is that posts_per_page is only returning one post no matter what number I set.

1

Don't nullify the main query, this breaks stuff. Also, avoid using the $wp_query global as a custom variable. This breaks the main query and causes issues. You can also try adding 'suppress_filters' => true, to your arguments to suppress any custom filters if you are using any custom filters.

Also, don't use TEMPLATEPATH, it is due to be depreciated according to a recent track ticket. Use get_template_part() instead

Try the following

<?php // The Query
    global $wp_query;

    $term = $wp_query->queried_object;

    $args=array(
        'meta_key' => 'views',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 5,
        'post_type' => 'post',
        'suppress_filters' => true,
        'tax_query' => array(
                array(
                    'taxonomy'  => $term->taxonomy,
                    'field'     => 'slug',
                    'terms'     => $term->slug,
                    )
                )
        );

    $new_query = new WP_Query($args);

    while ($new_query->have_posts()) : $new_query->the_post(); ?>

    <?php get_template_part( category-slider ); ?>

<?php
    endwhile;
    wp_reset_postdata(); ?> 

Apart from this, everything should work, except if you are using query_posts somewhere on the page which will break everything

  • get_template_directory_uri broke the page. – Chozen Jan 21 '15 at 12:13
  • Switched it to get_template_part and that worked but it's still only returning one item. – Chozen Jan 21 '15 at 12:16
  • Oh crap, sorry about that, typo, posted the answer before leaving for work :-). What happens if you remove the tax query, do you get more posts back – Pieter Goosen Jan 21 '15 at 12:20
  • Tried it, still no luck. – Chozen Jan 21 '15 at 12:44
  • Ok, then your problem is with your meta key. Are these correct. Do you get the correct amount of posts if you remove the meta key and ordering. It is really strange that it is not working. If nothing works, I will test it all a bit later. Still at work – Pieter Goosen Jan 21 '15 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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