get or query posts by terms of taxonomy (701)
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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 ? ?> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?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 ?> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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 ?> |
本文更新网址:https://feilong.org/get-or-query-posts-by-terms-of-taxonomy
2011-02-17 ~ 2011-02-17
加入收藏夹,查看更方便。
分类: wordpress