Get The Post Type A Taxonomy Is Attached To

If we peek into the global $wp_taxonomies variable we see the associated object types. There might be better ways to do this or even core functions, but you could try the following: function wpse_172645_get_post_types_by_taxonomy( $tax = ‘category’ ) { global $wp_taxonomies; return ( isset( $wp_taxonomies[$tax] ) ) ? $wp_taxonomies[$tax]->object_type : array(); } then for the … Read more

Get Posts by Custom Post Type ,Taxonomy, and Term

This is the answer to the question 🙂 <?php $args = array( ‘post_type’=> ‘services’, ‘areas’ => ‘painting’, ‘order’ => ‘ASC’ ); $the_query = new WP_Query( $args ); if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // content goes here endwhile; wp_reset_postdata(); else: endif; ?>

Get the the top-level parent of a custom taxonomy term

Thanks to Ivaylo for this code, which was based on Bainternet’s answer. The first function below, get_term_top_most_parent, accepts a term and taxonomy and returns the the term’s top-level parent (or the term itself, if it’s parentless); the second function (get_top_parents) works in the loop, and, given a taxonomy, returns an HTML list of the top-level … Read more

Get terms by taxonomy AND post_type

Here is another way to do something similar, with one SQL query: static public function get_terms_by_post_type( $taxonomies, $post_types ) { global $wpdb; $query = $wpdb->prepare( “SELECT t.*, COUNT(*) from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p … Read more

Retrieve posts by term id custom query

Have you tried using the WP_Query class? You might find it’s easier to use the built-in tools for this instead of a custom query from scratch. Something similar to the following should work for you: <?php $args = array( ‘post_type’ => ‘recipe_cpt’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘recipe_tx’, ‘field’ => ‘term_id’, ‘terms’ => 37 … Read more