0

I'm trying to output a list of dates that are held in a meta key called event_dates without duplicates. I've also tried a wp_query with a meta_compare but I'm not able to make that work either. Any help would be greatly appreciated. Thanks!

<?php 
    $args1 = array(
        'post_type'=>'cw_events',
        'showposts'=>-1,
        'order'=>'ASC',                
    ); 

    $archive = get_posts( $args1 ); 

    foreach( $archive as $post ) : 
        setup_postdata($post);
        global $wpdb;

        $metatable = $wpdb->prefix."postmeta";   
        $post_id = $post->ID;    
        $cal_dates = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT meta_value 
                FROM $metatable 
                WHERE post_id=%d 
                AND meta_key='event_date'",$post_id
        ) );

        //output posts -- trying to use array_unique(), but it's not working    
        $cal_dates = array_unique($cal_dates);  
        foreach ($cal_dates as $cal_date) {      
            echo var_dump($cal_date);
            echo $cal_date->meta_value.'<br />';         
        } 
    endforeach; 
?>

I saw this post, but the method does not seem to work. There's no such function as get() in wp.... Group posts by meta_key

PS sorry for the messy code, SE seems to be having some trouble dealing with returns...

1

The answer by felipelavinz assumes that there is an array of dates stored in the post_meta of each post, but the way I read your question and sample code it seemed as though you have one date specified per post? If this is true (one date per post)

then the following would suit you better

<?php
// this is the correct equivalent to the get_posts call
// Don't use get_posts.
// you should also specify some "orderby" parameter
$entries = new WP_Query(array(
    'post_type' => 'cw_events',
    'posts_per_page' => -1,
    'order' => 'ASC'
));

dates = array(); //we will fill this up with dates in the loop

if ( $entries->have_posts() ) : while ( $entries->have_posts() ) : $entries->the_post();

    //WP_Post magic methods allow us to access meta values like so

    $dates[] = $post->event_date;

endwhile; endif;

$dates = array_unique( $dates );

// always user wp_reset_postdata() UNLESS interacting with the main query
wp_reset_postdata();
1

Ok, you can do this using direct queries with $wpdb or just with WordPress.

Generally speaking, if using WordPress, do it the WordPress way.

I'm assuming that all the dates are stored using the same format; if they're not, you might need to solve that first.

The problem with your code it's that you're using array_unique on an array of objects; if you're just interested on the dates, the solution it's way easier.

<?php
// this is equivalent to the get_posts call
// you should also specify some "orderby" parameter
$entries = new WP_Query(array(
    'post_type' => 'cw_events',
    'posts_per_page' => -1,
    'order' => 'ASC'
));

if ( $entries->have_posts() ) : while ( $entries->have_posts() ) : $entries->the_post();
    // the "false" param will make the function return an array
    $dates = get_post_meta( get_the_ID(), 'event_date', false);
    // ... and now they're unique
    $dates = array_unique( $dates );
endwhile; endif;

// return globals $post and $wp_query to it's prior state
wp_reset_query();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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