Wordpress, WP_Query with custom taxonomy and custom post type

This code receives 10 messages of all types associated with term_name;

global $wp_query; query_posts( array( "taxonomy_name" => "term_name", 'showposts' => 10 ) ); 

This code receives 10 messages of a custom message type message;

 global $wp_query; query_posts( array( 'post_type' => 'message' 'showposts' => 10 ) ); 

However, this code always ignores the post_type requirement, but still selects all message types associated with the term name;

 global $wp_query; query_posts( array( 'post_type' => 'message' , "taxonomy_name" => "term_name", 'showposts' => 10 ) ); 

I do not see how both can work individually, but together they do not, if this cannot be a mistake - any thoughts?

+4
source share
3 answers

Looks like a mistake. Have you tried a custom selection request ? This should do it:

 $querystr = " 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 = 'message' AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'taxonomy_name' AND $wpdb->terms.slug = 'term_name' ORDER BY $wpdb->posts.post_date DESC LIMIT 10 "; $pageposts = $wpdb->get_results($querystr, OBJECT); 

I used this answer when building the query.

+5
source

Submitted by - core.trac.wordpress.org/ticket/13020. The fix is ​​simply changing one line of the query.php file and the if statement to check if post_type is empty.

+1
source

Use 'category_name' instead of 'taxonomy_name'. Powered by WP 3.5.2

 query_posts( array( 'post_type' => 'message', 'category_name' => 'term_name', 'showposts' => '10' ) ); 
0
source

Source: https://habr.com/ru/post/1307050/


All Articles