• Resolved aintnosin

    (@aintnosin)


    I’m trying to return query the database for custom post_type ‘person’ and the post_meta field containing that person’s birthday.

    This query works in phpMyAdmin:

    $bd = $wpdb->get_results("
    SELECT ID, post_title,
    DATE('1970-01-01' + INTERVAL wp_postmeta.meta_value SECOND) AS birthdate,
    MAKEDATE(YEAR(CURDATE()), DAYOFYEAR(DATE('1970-01-01' + INTERVAL wp_postmeta.meta_value SECOND))) AS thisYear
    FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.ID=wp_postmeta.post_id
    WHERE wp_posts.post_type='person' AND wp_postmeta.meta_key='wpcf-date-of-birth'
    ", ARRAY_A);

    That produced a blank response from get_results, so I converted all the table references and tried this:

    $bd = $wpdb->get_results("
    SELECT ID, post_title,
    DATE('1970-01-01' + INTERVAL $wpdb->postmeta.meta_value SECOND) AS birthdate,
    MAKEDATE(YEAR(CURDATE()), DAYOFYEAR(DATE('1970-01-01' + INTERVAL $wpdb->postmeta.meta_value SECOND))) AS thisYear
    FROM $wpdb->posts
    INNER JOIN $wpdb->postmeta ON $wpdb->posts.ID=$wpdb->postmeta.post_id
    WHERE $wpdb->posts.post_type='person' AND $wpdb->postmeta.meta_key='wpcf-date-of-birth'
    ", ARRAY_A);

    I’m not getting error messages, I’m just getting a NULL return on the query, which works fine when I query the database directly in MySQL.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘get_results return empty.’ is closed to new replies.