Is it possible to use the Wordpress API (query_posts, WP_Query, get_posts etc) to fetch future posts based on a custom taxonomy?
I'm looking for a way to fetch posts in wordpress that are slated for future publication based on a custom taxonomy query. Finding future posts is otherwise, simple
query_posts('post_status=future');
But adding a custom taxonomy doesn't work as expected
query_posts('musicians=paul-mccartney&post_status=future');
I ended up using the $wpdb and writing the SQL by hand and joining the terms, taxonomy and relationships tables together.
$sql = "SELECT post.*
FROM {$wpdb->prefix}terms term
JOIN {$wpdb->prefix}term_taxonomy taxonomy
JOIN {$wpdb->prefix}term_relationships relationship
JOIN {$wpdb->prefix}posts post
WHERE term.term_id = taxonomy.term_id
AND relationship.term_taxonomy_id = taxonomy.term_taxonomy_id
AND term.slug = '%s'
AND taxonomy.taxonomy = '%s'
AND post.ID = relationship.object_id";
if($posts = $wpdb->get_results( $wpdb->prepare($sql, $term->slug, $term->taxonomy) )):
foreach($posts as $post):
setup_postdata($post);
the_ID().开发者_开发知识库' '.the_title().'\n<br/>';
endforeach;
endif;
This works but I'm hoping that there is a way to accomplish the same but using the WP API (query_posts, WP_Query, get_posts etc)!
Yes you can.
$events = get_posts('numberposts=-1&post_type=events&musicians=paul-mccartney&post_status=future&order=ASC');
foreach($events as $event){
setup_postdata($event);
echo '<a href="'.get_permalink($event->ID).'">'.$event->post_title.'</a>';
}
You can use the same parameters as in, http://codex.wordpress.org/Function_Reference/WP_Query#Parameters
精彩评论