I have a own "get_calendar" function (plugin) cloned from the default wp function. I just wanna see posts that have a certain custom field set (true). How do I add this goal into:

$gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");

the name of the custom field checkbox is


So if this field returns true, I want those posts to show in the calendar!


The line you quote doesn't actually query any posts. It's just a very general check if there are any posts at all ever.

The issue with parts of WP core like this, is that WordPress is engineered to query sets of posts. Whenever said sets might get extremely large — it mostly has to abandon WP_Query with all the functionality it has and go for raw SQL.

It's not that hard to generate conditions for set of posts scenario for this (not tested, might also need type if your values are boolean):

$condition = array(
        'key'   => 'ua_allow_future_check',
        'value' => true,

$meta_query = new WP_Meta_Query( $condition );
$sql        = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );

var_dump( $sql );

Gives us:

    [join] =>  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    [where] =>  AND ( (wp_postmeta.meta_key = 'ua_allow_future_check' AND CAST(wp_postmeta.meta_value AS CHAR) = '1') )

But bolting this to the fork of get_calendar() is separate and messy story.

  • So, is it not possible to add that somehow into : $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date), ID FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' AND post_type = 'post' AND post_status = 'publish' AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); – Jonas Lundman Sep 8 '14 at 11:18
  • I aslo need it for this sql: $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' AND post_type = 'post' AND post_status = 'publish'"); – Jonas Lundman Sep 8 '14 at 11:22
  • Well, it is likely "somehow" possible, but MySQL isn't my forte so this is as far as I can get you, before running away screaming. :) – Rarst Sep 8 '14 at 11:28
  • So noone can help me? – Jonas Lundman Sep 9 '14 at 4:02

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.