I'm trying to figure out doing a custom query using the $wpdb Object.

I have custom type posts, event_posts, that have meta data, images, as well as event dates which are input in the post with the FT Calendar plugin.

I can get posts that have event dates (dates pulled from the ftcalendar_events table), but my first issue is that when I query the post type it comes back as "revision" and not "event_posts".

My second issue is that I don't know how to retrieve the metadata and images for the post.

Here's what I am using below.

Why am I doing it this way you might ask? Because I need to order the posts based on the event date, which is pulled from the ftcalendar_events table, and even though the standard wp query methods retrieve the post with all of that information, there isn't a way to order them by the event date.

Any ideas how to retrieve all of that?

global $wpdb;
$poos = $wpdb->get_results("
    p.post_title AS title,
    p.post_type AS type,
    p.post_content AS content,
    ev.start_datetime AS start
    " . $wpdb->prefix . "posts AS p,
    " . $wpdb->prefix . "ftcalendar_events AS ev
    p.post_parent = ev.post_parent

//echo '<br><br><br>';

foreach($poos as $poo){
    echo $poo->type.'<br>';
    echo $poo->title.'<br>';
    echo $poo->content.'<br>';
    echo $poo->start.'<br><br><br>';    

That outputs:

revision (should be "event_posts")
Home Town Celebration
Historic 'Home Town' Celebration in Ridgefield WA. An '0ld town' holiday celebration artisans fair and market. December 2013
2013-12-07 11:00:00
  • 3
    For your first issue -- add AND p.post_type='event_posts' to your WHERE clause. Also: I'm fairly certain there's a much easier way to do what you're trying to do -- have a look at get_posts() and/or WP_Query. – Pat J May 1 '13 at 21:09
  • Woops, forgot to add that for this, just added that back in. Still I don't know how that is possible with WP_Query to order by even date when it's from another table. Do you? – dragonfeet2012 May 1 '13 at 22:12
  • No, I think you're probably right. If you need to get data from a non-standard WP table, you need to use $wpdb. – Pat J May 2 '13 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.