Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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("
    SELECT
    p.post_title AS title,
    p.post_type AS type,
    p.post_content AS content,
    ev.start_datetime AS start
    FROM
    " . $wpdb->prefix . "posts AS p,
    " . $wpdb->prefix . "ftcalendar_events AS ev
    WHERE
    p.post_parent = ev.post_parent
    AND
    p.post_type='event_posts'
    ORDER BY
    ev.start_datetime
    ASC
");

//print_r($poos);
//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
share|improve this question
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.