• Hey there, this one’s a challenge. I’m not even quite sure I’m going about this right. I’m trying to build a calendar in WordPress for a client. What I essentially need to do is parse a series of posts for a particular meta key, which contains the date and extrapolate unique dates from those posts. From this loop I should be able to organize the posts into their respective date rows (that part I’ve got figured out).

    I hope that makes sense here’s what I have so far, I’m not entirely sure this is even the correct direction:

    <?php
    $args= array(
                    'post_type'=>'cw_events',
                    'showposts'=>-1,
                    'order'=>'ASC',
                );
                $archive = get_posts( $args );
    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, SORT_REGULAR);
    
    		foreach ($cal_dates as $cal_date) {
    
    		    echo var_dump($cal_date);
    		    echo $cal_date->meta_value.'<br />';
    
    		}
    
    endforeach; ?>

    So far it just outputs all of the data from each of the posts without parsing it for duplicates… Not sure what to do.

    Thanks for the help in advance!

  • The topic ‘Post Meta Key sorting – removing duplicates’ is closed to new replies.