1

i have a problem when trying to show a current 4 post using WP_Query. But, the older post (The very first post) also shown in the first loop.

This is my code:

<?php
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => '3'
    );
    $query = new WP_Query( $args ); //show 4 post
    if ( $query->have_posts() ){
        /* Start the Loop */
        while ( $query->have_posts() ) {
            $query->the_post();?>
            <div class="col-md-3 d-flex align-items-stretch">
                <div class="card ">
                    <?php if (has_post_thumbnail()) { 
                        $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); 
                        ?>
                        

                        <img class="miniimg" src="<?php echo $featured_img_url;  ?>" width="auto" height="200px">
                    <?php } ?>
                    <div class="card-body">
                        <h4><a href="<?php the_permalink(); ?>">
                            <?php the_title(); 
                            $post_date = get_the_date( 'j F Y' );
                            ?>
                        </a></h4>
                        <p class="card-text"><?php the_excerpt();?></p>
                        <div class="d-flex justify-content-between align-items-center">
                            <div class="btn-group">
                                <button type="button" class="btn btn-sm btn-primary">Read More</button>
                            </div>
                            <small class="text-muted"><?= $post_date; ?></small>
                        </div>
                    </div>
                </div>
            </div>
            <?php 
            $counter++; }
        }
        ?>

This is the result => Result image

How to fix this problem? is there any problem with my code? Thankyou.

4
  • How does the $args array look like for real? The definition you have attached has a limit of 3, so it is not exactly the version you generated the screenshot with... Commented Jan 2, 2021 at 16:47
  • @ZoliSzabó sorry, i have changed it. I mean i want to show 4 last post with that query. 'posts_per_page' => '3' it will show 4 post. but my problem is, the very first post is also shown in the first loop. you can see on the attached image. Commented Jan 2, 2021 at 17:10
  • @torsinta12 Glad I could help, don't forget to "accept the answer" too, regards. Commented Jan 2, 2021 at 17:16
  • 1
    wordpress.stackexchange.com/questions/958/… Commented Jan 2, 2021 at 17:49

3 Answers 3

3

If specifying 'posts_per_page' => 3 gives back 4 posts, it is almost 100% sure that the first post is a sticky post. Use the option ignore_sticky_posts to ignore them.

    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => 4,
        'ignore_sticky_posts' => 1,
    );
    ...
Sign up to request clarification or add additional context in comments.

Comments

2

A Sticky Post is the post will be placed at the top of the front page of posts. This feature is only available for the built-in post type post and not for custom post types.

I'm pretty sure you applied is sticky to your post from 2012.

To verify you can just add the following to your loop inside the while statement:

<?php //...
while( have_posts() ): the_post();
  if( is_sticky() ):
    echo 1;
    else: echo 0;
  endif;
endwhile;
//... ?>

Or go to you post in the admin console, and verify that you didn't check the "is sticky" checkbox on the right side in the publish panel.

Comments

0
<?php
$args = array( 'post_type' => 'movies');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><div class="movie-content" >
    <?php 
        echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">' ?>
        <h2><?php echo the_title(); ?></h2>
        <div><?php the_post_thumbnail('thumbnail'); ?></div>
        <div class="excerpt"><?php the_excerpt(); ?></div>
        <?php
        the_content();
        echo '<div class="entry-content">';
        echo '</div>';
        echo "</div></a>";
        endwhile;
        ?>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.