0

I cannot get this query to work properly. It just renders one post even though I have three posts that are categories in the 'Firm News' and 'Test' categories.

Can anyone see a problem with my code?

<?php
            $myquery = array(
                'post_type' => 'post',
                'tax_query' => array(
                    'relation' => 'OR',
                         array(
                        'taxonomy' => 'category',
                        'field' => 'slug',
                        'terms' => array('test', 'firm-news'),  
                    )

                ),
                'posts_per_page' => 5, 
                'orderby' => 'RAND', 
);



            $m = new WP_Query( $myquery );

            if ( $m->have_posts() ) : $m->the_post();?>

                <ul><li <?php post_class();?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
             <?php endif; wp_reset_postdata(); ?>

Thanks

Matt

1
  • Afaik the relation only works if you got multiple arrays/arguments. Have you tried moving the second term to another arguments?
    – kaiser
    Jan 28, 2013 at 4:20

1 Answer 1

1

You aren't looping through the results. Here is your code:

$m = new WP_Query( $myquery );

if ( $m->have_posts() ) : $m->the_post();?>
       <ul><li <?php post_class();?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
<?php endif; 
wp_reset_postdata(); ?>

There is no Loop. You just check for the existence of posts, echo some information about the first one and quit. if is not a Loop. It is a conditional. You need while.

You need:

$m = new WP_Query( $myquery );

while ( $m->have_posts() ) : $m->the_post(); ?>
       <ul><li <?php post_class();?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
<?php endwhile; 
wp_reset_postdata(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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