Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 . .

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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