• Hi

    Im using event calendar plugin where I can set event date for posts and it shows in calendar widget. Everything is working as inteded but I would also like to query post by event date (meta key start_datetime).

    Problem is event date is stored in separate table named wp_ftcalendar_events so regular query will not work.

    TABLE SCREENSHOT

    I came up with this code (reading through codex etc.) Tried a lot of things but still can’t make it work (not a programmer :/) Would someone be kind enough to help?

    <?php
    global $wpdb;
    global $post;
    $date = date("Y-m-d");
    $querystr = "
    SELECT *
    FROM wp_ftcalendar_events
    JOIN wp_ftcalendar_events on posts.ID = wp_ftcalendar_events.post_parent
    WHERE posts.ID = wp_ftcalendar_events.post_parent
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->wp_ftcalendar_events.meta_key = 'start_datetime'
    AND $wpdb->wp_ftcalendar_events.meta_value > " . $date . "
    ";
    
    $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
    <?php if ($pageposts): ?>  
    
    <?php global $post; ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>
  • The topic ‘Joining two tables and query by meta key’ is closed to new replies.