get or query posts by terms of taxonomy
feilong.org 修订于2011-02-17 06:51:24 394 次浏览How to get posts by terms of taxonomy ?For eaxmple,feilong want to get posts with the term slug 'zhangye'or 'linzhiying' in the taxonomy 'singer' ,Here is three ways http://feilong.org/get-or-query-posts-by-terms-of-taxonomy
'post',
'orderby'=>'count',
'posts_per_page'=>-1,
'singer'=> 'zhangye' //certain slug
);
$flposts = new WP_Query();
$flposts->query($args);
while ($flposts->have_posts()) : $flposts->the_post();
$postid=$post->ID;echo 'Post id is '.$postid;
the_title();
//the_category(', ');the_permalink();
//other stuff
endwhile;
//end of query posts by certain taxonomy term using WP_Query
//Feilong: Perhaps you try query posts(),or get_posts() instead ?
?>
null,
'post_status' => 'publish',
'taxonomy_name' => null,
'taxonomy_term' => null,
'orderby' => 'post_date',
'order' => 'DESC',
'numberposts' => '10'
);
$options = array_merge($defaults, $options);
extract($options);
//
//Format the post_type list so that it works in the query
$post_type = "('".str_replace(",","', '",$options['post_type'])."')";
//
//Get the term IDs from the Term Names
$terms = split('[,]', $options['taxonomy_term']);
$term_ids = $options['taxonomy_term'];
foreach($terms as $term){
$t_data = term_exists($term, $taxonomy_name);
$term_ids = str_replace($term,"'".$t_data['term_id']."'",$term_ids);
}
$term_ids = "(".$term_ids.")";
//
//Build the query
$query = "SELECT $wpdb->posts.* FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE 1=1
";
if($options['taxonomy_name'] != null){$query .="AND $wpdb->term_taxonomy.taxonomy = '".$options['taxonomy_name']."'
";
if($options['taxonomy_term'] != null){$query .="AND $wpdb->term_taxonomy.term_id IN $term_ids
";}
}
if($options['post_type'] != null){$query .="AND $wpdb->posts.post_type IN $post_type
";}
$query .="AND $wpdb->posts.post_status = '".$options['post_status']."'
";
$query .="GROUP BY $wpdb->posts.ID
";
$query .="ORDER BY $wpdb->posts.".$options['orderby']." ".$options['order'];
$my_posts = $wpdb->get_results($query);
return($my_posts);
}
//An example of using get_posts_by_taxonomy() within a loop:
$args = array(
'post_status' => 'publish',
'taxonomy_name' => 'singer',
'taxonomy_term' => 'zhangye, linzhiying'//Feilong: string,not array!
);$custom_posts = get_posts_by_taxonomy($args);
if ($custom_posts):
foreach ($custom_posts as $post): setup_postdata($post);
the_title();
//the_category(', ');the_permalink();
//other stuff
endforeach; else : endif;
//end of get posts by Taxonomy terms
?>
'post',
'orderby'=>'count',//or name
'posts_per_page'=>-1,
'tax_query' => array(
array('taxonomy' =>'singer','field' =>'slug','terms' => array('zhengye','linzhiling') )
)
);
query_posts($args);//refer to codex.wordpress.org/Template_Tags/query_posts#Taxonomy_Parameters
if(have_posts) : while(have_posts()) : the_post();
$postid=$post->ID;echo 'Post id is '.$postid;
the_title();
//the_category(', ');the_permalink();
//other stuff
endwhile;else:endif;
//end of query posts by Taxonomy Parameters,available with Version 3.1
?>
更新网址:https://feilong.org/get-or-query-posts-by-terms-of-taxonomy
最初发布:20110217 06:51:24 feilong.org 于广州
加入收藏夹,查看更方便。
所在分类: wordpress