I have two custom post types: "storefront_location" and "services"

These are linked by a custom taxonomy "location"

I am building a post template for "storefront_location" and would like to display a listing of the "services" posts associated with any given storefront.

What I'm trying to achieve:

A "storefront-location" post, London, is categorized with "london" ("location" taxonomy). I would like that London "storefront-location" post to display all "services" which are also categorized with "london" ("location" taxonomy). Users could then click on each service to read about it, since these are post types.

I'm trying to get the taxonomy information of the current post (London) and use tax-query to find all the services that share the same taxonomy with that specific London post.

Note: this is for a WP_query filter in Elementor, so the extra code for the Elementor query is from their tutorial here.

I'm fairly new to this, so there might be a newbie problem here. I've been trying to figure this out from other code examples here on stack exchange. I'd appreciate any help!

add_action( 'elementor/query/my_filter', function( $query ) {
//get the storefront-location post's location
   $custom_terms = wp_get_post_terms($post->ID, 'location');

if( $custom_terms ){

    //hold tax query parameters
    $tax_query = array();

    // loop through locations and build a tax query
    foreach( $custom_terms as $custom_term ) {

        $tax_query[] = array(
            'taxonomy' => 'location',
            'field' => 'slug',
            'terms' => $custom_term->slug,


    // put all the WP_Query args together
    $args = array( 'post_type' => 'services',
                    'posts_per_page' => -1,
                    'tax_query' => $tax_query );

    // finally run the query
    $loop = new WP_Query($args);

    if( $loop->have_posts() ) {

        while( $loop->have_posts() ) : $loop->the_post(); ?>

        <div class="listing-title"><?php the_title(); ?></div>     
        <?php endwhile;



EDIT: To clarify, the code show above doesn't seem to do anything at all when added to functions.php and when using 'my_filter' on the Posts widget. So, the London post shows all "services" and not just the services categorized with the location "london."

  • What exactly is the problem? You don't specify.
    – vancoder
    Commented Jul 19, 2021 at 20:33
  • When added to functions.php, it appears that this code doesn't actually do anything at all. For example when building the London page, the post query just displays all "service" posts, not just "service" posts categorized as the location "london". I am at a loss as to why; if anyone more experienced sees incorrect code I would really appreciate any insights!
    – srack
    Commented Jul 20, 2021 at 16:10

1 Answer 1


I was building something similar a couple of years ago, here's what I did, maybe it helps ... (changed to your use case)

$result = new WP_Query([
    'post_type' => 'location',
    'orderby' => 'name',
    'order' => 'ASC',
    'posts_per_page' => -1, // all of them on one page
    'tax_query' => [
            'taxonomy' => 'services',
            'field' => 'id',
            'terms' => $custom_term->term_id,
            'include_children' => true

So you already are pretty close. What I noticed is, you use $custom_term->slug but it should be $custom_term->term_id as far as I know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.