0

I'm using Event Calendar 3. Since there seems to be no easy way of querying all upcoming events as posts, I tried it this way, getting the IDs from the DB and querying the posts from that:

<?php if(!ec3_check_installed(__('Upcoming Events','ec3')))
    return;
global $ec3,$wpdb,$wp_version;

$calendar_entries = $wpdb->get_results(
    "SELECT
        p.id AS id
    FROM $ec3->schedule s
    LEFT JOIN $wpdb->posts p ON s.post_id=p.id
    WHERE p.post_status='publish'
    AND end>='$ec3->today' $and_before
    ORDER BY start $limit_numposts", OBJECT_K
); 

$args = array(
    'post_type' => 'post',
    'post__in' => $calendar_entries,
    'orderby' => 'id',
    'order' => 'DESC'
);

$query = new WP_Query( $args );

It doesnt work, obviously because the array looks like this, if I var_dump it:

array(2) {
    [37]=> object(stdClass)#2183 (1) { 
        ["id"]=> string(2) "37"
    }
    [8]=> object(stdClass)#2186 (1) { 
        ["id"]=> string(1) "8" 
    } 
} 

How can I use this array for my query?

0

Try this after your $wpdb query:

// collect calendar id's
$ids = array();
foreach( $calendar_entries as  $calendar_entries):
        array_push( $ids, $calendar_entries->id );
endforeach;

// query the above calendar id's
$args = array(
    'post_type' => 'post',
    'post__in' => $ids,
    'orderby' => 'id',
    'order' => 'DESC'
);
$query = new WP_Query( $args );

You can also modify the WP_Query by using posts_where, posts_join and posts_clauses.

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.