• Resolved michaelware1205

    (@michaelware1205)


    My second stumper of the day.

    On my single.php template, I’m displaying the usual content and several fields created with Advanced Custom Fields. All of that data displays correctly, but when I try to show related posts from the same category, I get nothing, even though I know 2-3 related posts exist in that category.

    I’ve done this type of related posts query on several sites, as have billions of other developers. But I don’t completely understand when I do and don’t need to reset the post data and feel like that may be the issue.

    Any obvious issues with this query?

    <?php  
    // related stories category info passed in from single.php or template-cat-landing.php
    // $category_name is defined at the top of the page
    
    $currentID = get_the_ID();
    
    $args = array( 
      'category_name' =>  $category_name,
      'orderby' => 'date',
      'post_type'=> 'post',
      'posts_per_page' => -1,
      'post__not_in' => array($currentID),
    
    );
    
    $the_query = new WP_Query( $args );
    // The Loop
    
    //if ( $the_query->have_posts() ) : ?>
    
        <section id="all-stories" class="layer">
            <div class="control-wrapper">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="row">
                            <div class="col-xs-12">
                                <h2 class="h3 tapered-lines special-text pad-bottom">
                                    Browse All of Our <?php echo $category_name; ?> Stories 
                                </h2>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12">
                                <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                
                                        <div class="story-card-wrap">
                                            <div class="story-card">
                                                <a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); ?>       
                                                    <div class="info">
                                                        <div class="content-label"><?php echo $category_name; ?></div>
                                                        <h3><?php the_title(); ?></h3>
                                                    </div>
                                                </a>
                                            </div><!-- /story-card -->
                                        </div><!-- /story-card-wrap -->
    
                                <?php endwhile;  ?>
                                
                            </div>
                            
                        </div>
                        
                    </div>
                </div>
            </div><!-- end control-wrapper -->
        </section><!-- end section -->
    
    <?php 
    //endif;
    // Reset Post Data
    wp_reset_postdata();
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter michaelware1205

    (@michaelware1205)

    I found a solution.

    Instead of using the category name, I used the category ID. I’m not sure why can cannot rely solely on the category name, but ID worked for me.

    Here is the revised code:

    <?php  
    // related stories category info passed in from single.php or template-cat-landing.php
    
    $currentID = get_the_ID();
    $category_id = get_cat_ID( $category_name );
    
    $args = array( 
      'cat' => $category_id,
      'orderby' => 'date',
      'post_type'=> 'post',
      'posts_per_page' => -1,
      'post__not_in' => array($currentID),
    );
    
    $the_query = new WP_Query( $args );
    
    // The Loop
    
    //if ( $the_query->have_posts() ) : ?>
    
        <section id="all-stories" class="layer">
            <div class="control-wrapper">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="row">
                            <div class="col-xs-12">
                                <h2 class="h3 tapered-lines special-text pad-bottom">
                                    Browse All of Our <?php echo $category_name; ?> Stories 
                                </h2>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12">
                                <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                
                                        <div class="story-card-wrap">
                                            <div class="story-card">
                                                <a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); ?>       
                                                    <div class="info">
                                                        <div class="content-label"><?php echo $category_name; ?></div>
                                                        <h3><?php the_title(); ?></h3>
                                                    </div>
                                                </a>
                                            </div><!-- /story-card -->
                                        </div><!-- /story-card-wrap -->
    
                                <?php endwhile;  ?>
                                
                            </div>
                            
                        </div>
                        
                    </div>
                </div>
            </div><!-- end control-wrapper -->
        </section><!-- end section -->
    
    <?php 
    //endif;
    // Reset Post Data
    wp_reset_postdata();
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query returns nothing’ is closed to new replies.