1
votes

I'm building a template for a homepage which shows 4 latest posts on top and some groups of posts divided by category around the page (I'm using get_posts to perform queries).

What I'd like to do is exclude from these category posts any post already present in the latest four news on top.

I guess I should get that four post IDs and use the 'post__not_in' parameter in the "category" queries, but I can't make it work.

Dou you have any hints?

This is the code:

// First query: I get last four posts

$args = array(

    'post_type'        => 'post',
    'post_status'      => 'publish',
    'numberposts'           => 4

);

// In my dreams, this should be the array of post IDs

$ids->query_vars['page_id'];

// Second query: I get 5 posts for a given categoory and exclude posts with IDs from first query

$query = new WP_Query($args);

$args2 = array(

    'numberposts'   => 5,
    'category'         => 15,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post__not_in' => $ids

);
$query2 = new WP_Query($args2);

$primaposizione = get_posts( $args2 );
foreach ( $primaposizione as $post ) : setup_postdata( $post );

... do the stuff ...

endforeach;
wp_reset_postdata();

UPDATE

<?php
    $args = array(
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'posts_per_page'           => 4
    );

    $query = new WP_Query($args);
    $excludeID = array();
    while ( $query->have_posts() ) : $query->the_post();
    $excludeID = $post->ID;
    endwhile;

        $args = array(
            'posts_per_page'   => 1,
            'orderby'          => 'post_date',
            'order'            => 'DESC',
                    'category'         => 15,
            'post_type'        => 'post',
            'post_status'      => 'publish',
            'post__not_in' => array($excludeID)
        );

        $primaposizione = get_posts( $args );
        foreach ( $primaposizione as $post ) : setup_postdata( $post );
        $category = get_the_category();
        $slug = $category[0]->category_nicename ;
        $esteso = $category[0]->cat_name;
        if(has_post_thumbnail()) { ?>
            <span class="hidden-xs"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('bones-thumb-300', array('class' => 'img-responsive')) ?></a></span>
            <?php } ?>
            <h3 class="ellipsis"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt();?>
        <?php endforeach;
    wp_reset_postdata();
?>
1
You're on the right track. Please post some code to comment on. - Koen de Bakker
You're right, I just pasted my code. - digitalfaber

1 Answers

0
votes

The way you're trying to select the ID's is not going to work. You have to call wp_query first. I think this should work:

$args = array(
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'numberposts'           => 4
);

$query = new WP_Query($args);
$excludeID = array();

while ( $query->have_posts() ) : $query->the_post(); ?>
     $excludeID[] = $post->ID;       // forgot the brackets
     // do stuff
endwhile;

(...)