0

I need to display "Featured posts" randomly for each day of the week on a single page. Posts need to be current.

What I want to do: I load 7 posts in a variable using WP_Query, test each post to see if it's current, and display only the remaining ones.

Problem: I can't find how to delete an entry in a while loop.

Init of the page:

$args = array(
            'post_type' => 'listing',
            'post_status'  => 'publish',
            'post__not_in' => array($post->ID),
            'orderby' => 'date',
            'order' => 'DESC',
            'meta_key' => 'featured_calendar',
            'meta_value' => 'on',
            'posts_per_page' => 7
        );      
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();

$theending = get_post_meta($post->ID, 'theend', true);

        if ($theending > time()){
             echo "All Good";
        } else{
             //Delete the entry
        }

endwhile;
endif;

Inside the days of the week loop:

if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
   //Display of remaining posts
endwhile;
endif;
2
  • Why do you need to delete it? Do you mean you need to actually delete it from the database? Commented Jan 14, 2016 at 15:43
  • I'd like to only delete it from $featured, the array of posts, so that I end up with an array of current posts only Commented Jan 14, 2016 at 15:45

3 Answers 3

1

It sounds like you should just modify the query to exclude those posts in the first place:

$args = array(
    'post_type' => 'listing',
    'post_status'  => 'publish',
    'post__not_in' => array($post->ID),
    'orderby' => 'date',
    'order' => 'DESC',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'featured_calendar',
            'value' => 'on',
            'compare' => '='
        ),
        array(
            'key' => 'theend',
            'value' => time(),
            'compare' => '>',
            'type' => 'NUMERIC'  // Not sure if you want a TIME here
        ),
    ),
    'posts_per_page' => 7
); 
$featured = new WP_Query($args);

Read more about custom field parameters in the Codex.

Sign up to request clarification or add additional context in comments.

1 Comment

Works great! Thanks so much for this solution!
0

If you're looking to remove a row from an array, you can do that like this:

unset ($array[$key]);

1 Comment

I tried using $key = key($featured); unset($featured[$key]); but without success
0
$recent_featured = array();
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
    $theending = get_post_meta($post->ID, 'theend', true);
    if ($theending > time()){
         $recent_featured[] = $post;
    } else{
    }
endwhile;

now you can use $recent_featured, pass it, loop over it, display them...

5 Comments

Sorry that doesn't seem to work. Doesn't crash my code when I make $recent_featured a new empty WP_Query, but still not recognized as having posts
It's not a WP_Query, it's an array of posts. If it's empty its because of your condition.
Sorry I was guessing for the WP_Query, but if $featured is an array, then $recent_featured[] = $post; makes the page crash
No, $featured is WP_Query, $recent_featured is an array
sorry, I meant recent_featured in my last answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.