0

I am newbie in wordpress development, and usually I use query posts with simple commands.

I have a slider in home page which shows 3 latest posts form 'featured' category. I have also a news box category which shows 3 latest posts form 'news' category.

for rest of my home page I want to show 10 posts. but want to exclude 3 latest posts form 'featured' category and 'news' category becuase I don't want posts be repetitive (duplicate) in home page.

any help is appreciated.

  • 3
    What have you coded so far? Please post your work. Also note query_posts is not recommended. – Brad Dalton Jul 21 '15 at 3:34
  • 1
    query_posts breaks the main query object on which the content of a page relies. There is no use in posting any kind of solution when you are using query_posts on the affected page :-) – Pieter Goosen Jul 21 '15 at 5:00
1

I'd do it like this:

$excluded_posts = array();

// select first 3 featured posts ordered by date

$args = array (
    'post_type'              => array( 'post' ),
    'category_name'          => 'featured',
    'posts_per_page'         => '3',
    'order'                  => 'DESC',
    'orderby'                => 'date',
    'ignore_sticky_posts'    => true, // or maybe not?
);

$query = new WP_Query( $args );

while ( $query->have_posts() ) { $query->the_post();
    $excluded_posts[] = get_the_ID();
}

wp_reset_postdata();

// select first 3 news ordered by date

$args['category_name'] = 'news';

$query = new WP_Query( $args );

while ( $query->have_posts() ) { $query->the_post();
    $excluded_posts[] = get_the_ID();
}

wp_reset_postdata();

$args = array (
    'post__not_in'              => $excluded_posts,
    'ignore_sticky_posts'       => true,
);

$query = new WP_Query( $args );

while ( $query->have_posts() ) { $query->the_post();
    // finally loop with last 10 without last 3 news and featured posts
}

wp_reset_postdata();
| improve this answer | |
  • Use pre_get_posts, much cleaner – Pieter Goosen Jul 21 '15 at 7:10
  • Do not replace the main query with a custom one – Pieter Goosen Jul 21 '15 at 7:11
  • Thanks Emetrop, It is misunderstood. My site have many categories: Featured, news, technology, Startup, ... I want to show 10 latest published posts per page from all categories, but do not show 3 latest posts of 'featured' category and 3 latest posts of 'news' category.(because 3 latest posts of 'featured' category and 3 latest posts of 'news' category exist in other section of home page) – Asghar Jul 21 '15 at 7:26
  • Asghar I edited the code right now. It'll work but it's not the best practice as Pieter Gossen said, I'll edit it later. – Emetrop Jul 21 '15 at 7:36
  • Thank you Emertop, it works fine. I do not know why wp-pagenavi is not working with the query you wrote. I put <?php wp_pagenavi(); ?> before the wp_reset_postdata(); but nothing is displayed. – Asghar Jul 21 '15 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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