1

I have here a query which display if it matches the current month and year published. The second one need to be the reverse one for the first code which display all post except the one that match the current month and year.

// CURRENT MONTH AND YEAR ONLY
    <?php
        $current_year = date('Y', current_time('timestamp'));
        $current_month = date('m', current_time('timestamp'));
        if ( have_posts() ) { while ( have_posts() ) { the_post(); }} 
        $args = array('post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => 12, 'post_parent' => 11120, 'orderby'=> 'menu_order', 'year' => $current_year, 'monthnum' => $current_month, 'order' => 'DESC');
        $query = new WP_Query($args);
        $catNull = get_template_directory_uri();
        while ($query->have_posts()) {
            $query->the_post(); ?>
                <li class="active MonthTreats slide">
                    <div class="MonthHeader">
                        <h1><?php the_title(); ?></h1>
                    </div>
                    <div class="Content">
                        <div class="MonthContent">
                            <div class="container">
                                <p class="Status">NOW AVAILABLE</p>
                                <?php echo c2c_get_custom('Short Description', $before='<h2>', $after='</h2>'); ?>
                                <?php the_content(); ?>
                            </div>
                        </div>
                    </div>
                </li>
            <?php
        }
        wp_reset_postdata();?>


// ALL POST EXCEPT CURRENT MONTH AND YEAR
    <?php
        if ( have_posts() ) { while ( have_posts() ) { the_post(); }} 
        $args = array('post_type' => 'page', 'post_status' => 'publish, future', 'posts_per_page' => 12, 'post_parent' => 11120, 'orderby'=> 'menu_order', 'order' => 'asc');
        $query = new WP_Query($args);
        $catNull = get_template_directory_uri();
        while ($query->have_posts()) {
            $query->the_post(); ?>
                <li class="MonthTreats slide">
                    <div class="MonthHeader">
                        <h1><?php the_title(); ?></h1>
                    </div>
                    <div class="Content">
                        <div class="MonthContent">
                            <div class="container">
                                <p class="Status">NOW AVAILABLE</p>
                                <?php echo c2c_get_custom('Short Description', $before='<h2>', $after='</h2>'); ?>
                                <?php the_content(); ?>
                            </div>
                        </div>
                    </div>
                </li>
            <?php 
        }
        wp_reset_postdata();?>
    </ul>

1 Answer 1

2

Initialize an array before the first loop:

$exclude = array();

Then add the ID of each post within the first loop to that array:

while ($query->have_posts()) {
    $query->the_post();
    $exclude[] = get_the_ID();
    // etc...

Then use that array to exclude those IDs via post__not_in in the second query:

$args = array(
    'post_type' => 'page',
    'post__not_in' => $exclude,
    // etc...
1
  • Thanks I never thought of using the first loop to exclude that from the second loop. works great!
    – MIke
    Nov 25, 2016 at 3:12

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.