是否可以使用Wordpress API(query_posts,WP_Query,get_posts等)根据自定义分类法获取未来的帖子?

时间:2010-06-24 16:17:29

标签: php sql wordpress

我正在寻找一种方法来获取wordpress中的帖子,这些帖子将根据自定义分类查询在未来发布。找到未来的帖子是 否则,简单

query_posts('post_status=future');

但添加自定义分类法无法按预期工作

query_posts('musicians=paul-mccartney&post_status=future'); 

我最终使用$ wpdb并手动编写SQL并将术语,分类和关系表连接在一起。

$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;

这有效,但我希望有一种方法可以实现相同但使用WP API(query_posts,WP_Query,get_posts等)!

1 个答案:

答案 0 :(得分:1)

是的,你可以。

$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>';
}

您可以使用与中相同的参数, http://codex.wordpress.org/Function_Reference/WP_Query#Parameters