0

Currently in my theme i am running the default query that pulls all recent posts/latest entries/latest published posts . But i am looking for something more complex like i am gonna setup two sections . One is for all the latest entries and the other one is for the four sub-sections which will show posts from 4 selected categories but the posts of those selected categories wont be showed in the latest entries i mean the first query which was pulling all posts will exclude the posts of 4 selected categories . cause there will be duplication ..

but i don't know the exact query/wp-query code for this type of setting . .

1 Answer 1

0

You must first do the 4 queries for selected categories and while looping through them remember the IDs. Save them in an array. Then when pulling latest posts just add the post__not_in parameter with all the IDs you want to exclude.

You need 4 queries that look somewhat like this:

    $args = array(
        'category_name' => 'category1'
    );
    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post();
        //do something
        $excludeIDs[] = the_ID();
endwhile;
wp_reset_postdata();

and then one something like this:

    $args = array(
        'post__not_in' => $excludeIDs
    );
    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post();
        //do something else
        $excludeIDs[] = the_ID();
endwhile;
wp_reset_postdata();

That should get you started. If you need any other help, just ask.

3
  • thanks for the answer . but can you demonstrate an example code? i am not a query pro and i only know some basic things . Oct 21, 2012 at 15:16
  • What does your code look like now? some of this varies from theme to theme.
    – Liana Mir
    Oct 21, 2012 at 23:08
  • @SadiMahmud ok, I've updated my answer with code examples
    – Miha Rekar
    Oct 22, 2012 at 7:25

Your Answer

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

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