Skaddict
Skaddict

Reputation: 81

PHP: Delete entry in array of arrays

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;

Upvotes: 0

Views: 70

Answers (3)

rnevius
rnevius

Reputation: 27092

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.

Upvotes: 1

Gavriel
Gavriel

Reputation: 19237

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

Upvotes: 0

Glubus
Glubus

Reputation: 2855

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

unset ($array[$key]);

Upvotes: 0

Related Questions