繁体   English   中英

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

[英]Is it possible to use the Wordpress API (query_posts, WP_Query, get_posts etc) to fetch future posts based on a custom taxonomy?

我正在寻找一种方法来获取Wordpress中基于自定义分类法查询将来发布的帖子。 否则,查找将来的帖子很简单

query_posts('post_status=future');

但是添加自定义分类法并不能按预期工作

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

我最终使用$ wpdb并手工编写了SQL,并将terms,分类法和关系表连接在一起。

$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等)!

是的你可以。

$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中相同的参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM