Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Afaik the relation only works if you got multiple arrays/arguments. Have you tried moving the second term to another arguments? –  kaiser Jan 28 '13 at 4:20
add comment

1 Answer 1

up vote 1 down vote accepted

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(); ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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