Skip Navigation

[Resolved] PHP Example to User Parent Field as Parametric Search Filter

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 5.30 hours from now. Thank you for your understanding.

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 4 years, 5 months ago.

Assigned support staff: Christian Cox.


Until Maps allows for a radius search option, I need to use GeoMyWP to create a custom search form that includes some custom fields & the radius search option. That means that I need to customize/hack the php search form template of that plugin. I’m hoping you can help with something that’s easy in Views but I’m not sure how to do with php code.

How can I use a parent post’s field as a search filter with php code? The search results will display the child posts in the loop.

With Views I’ve seen that this can be done with nested views. Is that sort of thing possible with PHP code. If so, do you have an example code you could share?




Here's how I would approach this:
1. Query all parent posts that have the matching field, resulting in a subset of parent post IDs. Assuming 'brand' is the parent post type slug and 'blue' is the value you're searching for:

add_filter( 'wpv_filter_query', 'toolset_parent_meta_filter', 10, 3 );
function toolset_parent_meta_filter( $query_args, $view_settings, $view_ID ) {
  $args = array(
  'meta_key' => 'wpcf-fieldslug',
  'meta_value' => 1234,
  'meta_compare' => 'numeric',
  'post_type'  => 'brand'
$query1 = new WP_Query( $args );
$parents = wp_list_pluck( $query1->posts, 'ID' );

Change "fieldslug" and 1234 to match your slug and value, change 789 to match your View ID. This example uses a numeric comparison. More info about this and examples here:

2. Query all child posts that have a parent that matches one of these parent post ids. If the parent / child relationship is created in Types, then you can determine the parent post ID using the "_wpcf_belongs_parentslug_id" postmeta key. For instance, let's say you have a parent post type "brand" and a chid post type "car". Each car post that has a parent brand will include an entry in the postmeta table "_wpcf_belongs_brand_id".

$args2 = array(
       'post_type'  => 'car',
       'meta_query' => array(
    'key'     => '_wpcf_belongs_brand_id',
    'value'   => $parents,
    'compare' => 'IN',
                'type' => 'numeric'
$query2 = new WP_Query( $args2 );
$cars = wp_list_pluck( $query2->posts, 'ID' );

Now $query2 is a result set of all the matching child posts, and $cars is an array of their IDs.



Thank you for this example -- this is great. My post type is not created by Types. I’m targeting WooCommerce product variations, which according to this post are children of the post type, Product. hidden link

It’s seems that I should be able to target the post_parent ID from the post table.

How would you modify your code to target the parent since I can’t use the _wpcf_belongs_brand_id as the key?


Hmmm, I can help with code directly related to Toolset, but this doesn't appear to be related to Toolset at all anymore. Product relationships in WooCommerce can be queried according to their API and formats, and I'm not really qualified to help with that. A quick search led me to some examples:

If you need help integrating with a Toolset API, then do reach out here and I can give more assistance. Thanks!