0

I am building a WordPress website and I want to show post in it. But the problem is that, I want to exclude the first post in query. I have not much knowledge of PHP. So please help me to de this. This is the basic code that The theme is using;

<?php
$blog_args = array(
    'post_type'      => 'post',
    'posts_per_page' => 8
);

$blog_posts = new WP_Query( $blog_args );

while ( $blog_posts->have_posts() ) {
    $blog_posts->the_post();
}

wp_reset_postdata();

Please help me to do this

2 Answers 2

1

You can do this using general programming/common sense with a variable and an if statement, no WordPress knowledge is required.

If we create a variable named $skip_next and set it to true, then perform a check in the loop, we can set it to false and continue to skip the first.

$skip_next = true;
while ( $blog_posts->have_posts() ) {
    if ( $skip_next ) {
        $skip_next = false;
        continuel
    }

    $blog_posts->the_post();

Now because $skip_next is false on the second post it will not be skipped

0

Delete the first post form the posts property:

$blog_posts = new WP_Query( $blog_args );
unset( $blog_posts->posts[0] );
4
  • Thanks, but I want to exclude the first post in query. For example if the query is displaying post either category wise or by date just hide the first one. Pls help me in this problem. Commented Jun 20, 2023 at 16:24
  • 1
    Have you tried my answer? If so, please b specific on what you tried and what the result was.
    – Caleb
    Commented Jun 20, 2023 at 16:44
  • Thanks for your help. And yes I have tried that. But it only exclude post of given id. But all I need is to exclude first post. Not by id. The first post in query should not be shown. Commented Jun 20, 2023 at 17:49
  • Updated answer.
    – Caleb
    Commented Jun 20, 2023 at 17:51

Your Answer

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

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