Request some custom taxonomy terms in Wordpress 2.8?

I created a custom taxonomy with the name "technology", but I can’t ask for a few terms, for example, using categories or tags.

These DO queries work:

query_posts('tag=goldfish,airplanes'); query_posts('technologies=php'); 

However, none of the following works correctly:

 query_posts('technologies=php,sql'); query_posts('technologies=php&technologies=sql'); 

My goal: Show all messages with php technology and all messages with sql technology

Any ideas? Is it possible? Thanks!

+4
source share
9 answers

Obviously, query_posts cannot help in this particular situation. (Hopefully it will be added in future versions of Wordpress!) The solution is to use a custom select query, for example:

 SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'technologies' AND $wpdb->terms.slug = 'php' OR $wpdb->terms.slug = 'css' ORDER BY $wpdb->posts.post_date DESC 

More information can be found in Wordpress Codex: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

+10
source

This is a slightly deferred answer, but it was first on Google for β€œposts related to WordPress in a few terms,” so I thought I made my conclusions.

Since this question was published, Wordpress was modified to allow this type of request. This will give you a list of messages associated with any custom taxonomy term assigned to the object:

 $post_cats = wp_get_object_terms(get_the_ID(), 'video_category', array('fields' => 'ids')); $args=array( "tax_query" => array( array( "taxonomy" => "video_category", "field" => "id", "terms" => $post_cats ) ), 'post__not_in' => array(get_the_ID()), 'post_type' => 'video', 'posts_per_page' => 8, 'caller_get_posts' => 1 ); $related_by_cats = new WP_Query($args); 

This is my first contribution to SO, I hope it complies with the standards.

+6
source

It works? query_posts ('tag = bread + pastries + recipe)

From: http://codex.wordpress.org/Template_Tags/query_posts

+1
source

Ok, so here is my crack. It is a bit hacked, but it works. The big drawback is that any other query variables need to be re-added, because when you call several terms, the failure command issues all the query vases.

In addition, I have not tested this against multiple taxonomy queries. This only works within the framework of a specific taxonomy. Use at your own risk.

 function multi_tax_terms($where) { global $wp_query; if ( strpos($wp_query->query_vars['term'], ',') !== false && strpos($where, "AND 0") !== false ) { // it failing because taxonomies can't handle multiple terms //first, get the terms $term_arr = explode(",", $wp_query->query_vars['term']); foreach($term_arr as $term_item) { $terms[] = get_terms($wp_query->query_vars['taxonomy'], array('slug' => $term_item)); } //next, get the id of posts with that term in that tax foreach ( $terms as $term ) { $term_ids[] = $term[0]->term_id; } $post_ids = get_objects_in_term($term_ids, $wp_query->query_vars['taxonomy']); if ( !is_wp_error($post_ids) && count($post_ids) ) { // build the new query $new_where = " AND wp_posts.ID IN (" . implode(', ', $post_ids) . ") "; // re-add any other query vars via concatenation on the $new_where string below here // now, sub out the bad where with the good $where = str_replace("AND 0", $new_where, $where); } else { // give up } } return $where; } add_filter("posts_where", "multi_tax_terms"); 
+1
source

It’s kind of silly that after implementing custom taxonomies in WP, there are no built-in functions for using them at will, and the documentation is close to nonexistent. I was looking for a solution, this request solves it (and made my day). Thanks.

And yet, unfortunately, I'm too dumb (OOP blind) to turn it into a function, so I don’t repeat it all. I get: **Fatal error**: Call to a member function get_results() on a non-object

I think I do not know how to call $ wpdb from a function.

0
source

It should look like this:

  global $wp_query; query_posts( array_merge( array('taxonomy' => 'technologies', 'term' => array('sql', 'php')), $wp_query->query ) ); 

which works for custom post_types at least.

0
source

Hey, I also ran into one problem once. If you have many different values, you can do this as follows, instead of writing the original SQL query:

 $loop = new WP_Query(array('technologies' => 'php','technologies' => 'sql')); 

You can then loop through the wp_query object created here. Although this is a very old post, and I'm sure you already solved the problem. :)

0
source
  //equivalent to get_posts function brand_get_posts($args=array()){ global $wpdb; $sql = "SELECT p.* "; $sql.= " FROM $wpdb->posts p"; $sql.= " LEFT JOIN $wpdb->term_relationships term_r ON(p.ID = term_r.object_id)"; $sql.= " LEFT JOIN $wpdb->term_taxonomy term_t ON(term_r.term_taxonomy_id = term_t.term_taxonomy_id)"; $sql.= " LEFT JOIN $wpdb->terms terms ON(term_t.term_id = terms.term_id)"; $sql.= " WHERE 1=1 "; if(!empty($args['post_type'])){ $sql.= " AND p.post_type = '".$args['post_type']."'"; } $sql.= " AND p.post_status = 'publish'"; if(!empty($args['taxonomy'])){ $sql.= " AND term_t.taxonomy = '".$args['taxonomy']."'"; } if(!empty($args['terms'])&&is_array($args['terms'])){ $sql.= " AND terms.slug IN ('".implode(",",$args['terms'])."')"; } $sql.= " ORDER BY p.post_date DESC"; if(!empty($args['posts_per_page'])){ $sql.=" LIMIT ".$args['posts_per_page']; } if(!empty($args['offset'])){ $sql.=" OFFSET ".$args['offset']; } //echo '<h1>'.$sql.'</h1>'; return $wpdb->get_results($sql); } 
0
source

All Articles