get or query posts by terms of taxonomy
飞龙更新于 2011-02-17 06:51 加入书签 CTRL+D 有 40 个朋友来过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
<?php /**One way, new WP_Query() to get posts by certain taxonomy slug or post type **/ $args=array( 'post_type'=>'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 ? ?> |
<?php // Two,in functions.php. // refer to: wordpress.org/support/topic/query-posts-by-taxonomy /*------------------------------------------------------------------- Get Posts by a Taxonomy Terms by Benji Greig benjibuls@gmail.com ------------------------------------------------------------------- This function allows for a query to be made similar to get_posts but allows for filtering by 'taxonomy_name' and multiple taxonomy terms. You can filter the query by comma delimited lists of: - 'taxonomy_terms' - 'post_types' Additionally you can filter the query by 'post_status' and define the 'orderby' from any of the posts fields and set either ascending or decending. It's not perfect, but it's coming in real handy for making special queries. -------------------------------------------------------------------*/ function get_posts_by_taxonomy($args) { global $wpdb; // if ( is_array($args) ) $options = &$args; else parse_str($args, $options); // $defaults = array( 'post_type' => 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 ?> |
<?php /** Three, if you are using wp3.1 or above,Perhaps you can query posts by taxonomy terms?*/ // Feilong: available with Version 3.1 $args=array( 'post_type'=>'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 ?> |
维护小站,感谢赞赏。
联系飞龙,请转淘宝。
飞龙初发:
2011-02-17 06:51
本文更新网址:
https://feilong.org/get-or-query-posts-by-terms-of-taxonomy
所在目录: wordpress