How do you search for a post by custom taxonomy?

For the record I found an answer to this question in the end – I needed to use a meta query rather than a taxonomy query. The correct code ended up being this: $relatedBlogPostArgs = array( ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => 1, ‘order’ => ‘DESC’, ‘orderby’ => ‘date’, ‘meta_query’ => array( array( … Read more

Get list of CPT posts in *current* post’s taxonomy term

You can get the Packages of the current Proposal using wp_get_object_terms() and then pass some/all of those into a WP_Query. $terms = wp_get_object_terms( $post_id, ‘packages’, array( ‘fields’ => ‘ids’ ) ); $args = array( ‘post_type’ => ‘proposals’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘packages’, ‘terms’ => $terms ) ), ); $related = new WP_Query( $args … Read more

Extend taxonomy term page with other posts

Ok I find a solution. I use posts_where filter in pre_get_posts. With this filter I can remove the term related WHERE part from the default SQL query. function custom_loops($query) { if (is_tax( ‘service_photo_location’ )){ $query->set( ‘posts_per_page’, 10); $query->set( ‘post__in’, $ids); $query->set( ‘orderby’, ‘title’); $query->set( ‘order’, ‘ASC’); add_filter( ‘posts_where’, function ( $where ) { $where = … Read more

Use Tags to Query Associated Multiple Posts and Get The Average Of Custom Field Values

If you use those custom fields only for the company tagged posts, then it’s sufficient to retrieve the custom fields without worrying about posts and tags: $tags = array(‘Professional Rating’, ‘Efficiency Rating’, ‘Referral Rating’); foreach ($tags as $tag) { $result = $wpdb->get_col( “SELECT meta_value FROM wp_postmeta WHERE meta_key = ‘$tag'” ); $avg = empty($result) ? … Read more

How to show amount of post in a taxonomy with advanced custom fields?

You can use this code for getting number of posts in a taxonomy: $args = array( ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => ‘your-taxonomy-name’, ‘field’ => ‘slug’, ‘terms’ => ‘some-slug’, ) ) ); $query = new WP_Query( $args ); echo $query->post_count;