0

I'm needing a couple queries on front-page.php and am at a loss why this is not working. My first query works fine, however, my second one specifying the category (or even nothing at all) never returns anything. Any ideas?

<div class="row">
  <div class="details">
    <?php 
        if (have_posts()) : while (have_posts()) : the_post(); 
        the_content(); 
        endwhile; else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php 
        endif; 
        wp_reset_postdata(); ?>

    <?php
        $secondary_query = new WP_Query( 'category_name=students' );        
        echo '<ul>';
        if ($secondary_query->have_posts()) : while ($secondary_query->have_posts()) : $secondary_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
        endwhile; else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
   <?php 
        echo '</ul>';
        endif; 
        wp_reset_postdata(); ?>
        <ul>
</div>

1
  • Are you sure there is a category with slug students, and that there are posts attached to it? Saying "it is failing" is a bit too broad, there's no way we can diagnose the problem without more information. Have you tried disabling plugins? Aug 20, 2015 at 14:22

1 Answer 1

0

Maybe you can try with get_posts() and foreach loop for the second query instead of WP_Query and then you will have something like this:

<?php
$secondary_query = get_posts('category_name=students');
foreach($secondary_query as $secondary_post):
   echo "<li>" . get_the_title($secondary_post->ID) . "</li>";
endforeach;
?>

With above like style you don't need wp_reset_query() just make sure you pass the post ID to the functions

Your Answer

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

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