Question

I'm developing a plugin made on the original calendar. My wordpress is based on real events and I want to keep trace of theme on the calendar.

So I added a custom field in the post which is the end of the events (or the last day for subriscribe) with the value of a date of course (like 25/02/2010).

Now I want to display on the calendar all the events that ends on the days instead of the posts create in each days.
How to get the meta data value of the post in this query?

 // Get days with posts
$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
    FROM $wpdb->posts WHERE MONTH(post_date) = '$thismonth'
    AND YEAR(post_date) = '$thisyear'
    AND post_type = 'post' AND post_status = 'publish'
    AND post_date < '" . current_time('mysql') . '\'', ARRAY_N);

It have to select all the post with my custom meta value.

Hope I'm not doing this for nothing, I googled and no plugin with this function came out.

Was it helpful?

Solution

I think you can do a left join :

left join 
    $wpdb->postmeta my_field_meta on (p.ID = my_field_meta.post_id and 
                                      my_field_meta.meta_key = 'subscribe') 

Where "subscribe" it's the name of your custom field. So your code could be:

 $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(p.post_date)
    FROM $wpdb->posts as p
    LEFT JOIN 
        $wpdb->postmeta my_field_meta on (p.ID = my_field_meta.post_id and 
                                          my_field_meta.meta_key = 'subscribe')     
    WHERE MONTH(p.post_date) = '$thismonth'
    AND YEAR(p.post_date) = '$thisyear'
    AND p.post_type = 'post' AND p.post_status = 'publish'
    AND p.post_date < '" . current_time('mysql') . '\'', ARRAY_N);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top