• I am currently working through a wordpress course and I am building the homepage of the wordpress website and have ran into an issue. I am trying to use the WP Query class to create a featured post however, the post is not being displayed. Here’s my code.

    
    <section class="middle-area">
                <div class="container">
                    <div class="row">
                        <aside class="sidebar col-md-3 h-100">
    
                            <?php 
                            
                            // Require a file called sidebar-home.php
                            get_sidebar( 'home' ); 
    
                            ?>
                                
                        </aside>
                        <div class="news col-md-9">
                        <div class="row">
                            <div class="container">
                                <?php 
    
                                $featured = new WP_Query( 'post_type=post&posts_per_page=1&cat=4,11' );
    
                                if( $featured->have_posts() ):
                                    while( $featured->have_posts() ): $featured->the_post();
                                ?>
    
                                <div class="col-12">
                                    <?php get_template_part( 'template-parts/content', 'featured' ); ?>
                                </div>
    
                                <?php
                                    endwhile;
                                    wp_reset_postdata();
                                endif;
    
                                ?>
                            </div>
                        </div>
    
                        </div>                          
                    </div>
                </div>
            </section>
    

    The content-featured file

    
    <article <?php post_class( array( 'class' => 'featured' ) ); ?>>
    <h2><?php the_title(); ?></h2>
    <div class="thumbnail">
        <?php the_post_thumbnail( 'large', array( 'class' => 'img-fluid' ) ); ?>
    </div>
    
    <div class="meta-info">
        <p>
            by <span><?php the_author_posts_link(); ?></span> 
            Categories: <span><?php the_category( ' ' ); ?></span>
            <?php the_tags( ' Tags: <span>', ', ', '</span>' ); ?>
        </p>
        <p><span><?php echo get_the_date(); ?></p>      
    </div>
    <p><?php the_excerpt(); ?></p>
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code works when tested on my site. I did alter the category IDs to match my IDs, and I didn’t use a template part, I simply combined all the code. If there’s an issue with fetching the template part, my testing would not expose it.

    Add an else: echo 'Nothing found'; line just before the endif; for the if( $featured->have_posts() ): condition. If you then see “Nothing found” where the post should appear, the category IDs you provided for the query might be incorrect since those are the only things I changed in my test.

    You can remove the else: condition later if you want, I just want to see if the query is working as expected or not.

Viewing 1 replies (of 1 total)

The topic ‘Wp_Query not showing featured post’ is closed to new replies.