Skip Navigation

[Resolved] Filter by post relationship / repeatable field groups owner

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to add a front-end filter that allows Users to filter a View of parent posts by whether or not a child post exists.

Solution: There is no such filter built-in to Views, but you could use some custom code to achieve something similar. Create a custom field on the parent post and programmatically set the value of that post whenever child posts are created. Then filter based on that custom field.

Relevant Documentation:
https://toolset.com/forums/topic/filter-view-on-whether-child-posts-exist/

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by andrei-laurentiuP 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1206913

Tell us what you are trying to do?
Authors can create multiple PARENT CPTs. A PARENT CPT may have multiple CHILD CPT. I have created a view where all PARENT CPTs are listed for the author and I need the author to be able to filter the PARENT CPT in front-end by "IF PARENT CPT has any CHILD CPT or not".

I have tried to add a "Filter by post relationship / repeatable field groups owner" but this seems not to be very flexible, as it only displays all the CHILD CPTs in the database for further filtering.

Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-alter-the-wpv-control-post-relationship-with-wpv_filter_query/
Is there a similar example that we can see?

What is the link to your site?

#1207196

Hi, unfortunately there's no built-in front-end filter that will allow you to accomplish this effectively. One way you can work around it is to use a custom field on the parent post type to determine whether or not there are any child posts, and set up a front-end filter based on that custom field. A bit of custom code is required. I wrote some example code for another ticket you can adapt to your specific case: https://toolset.com/forums/topic/filter-view-on-whether-child-posts-exist/

Let me know if you have questions about that.

#1207427

So I have the following:
PARENT CPT - 'ep';
CHILD CPT - 'entity_portfolio'
with "Has Portfolio" relationship which translates to 'wpcf-has-portfolio'

I have added the below code to my functions.php but this crashes my website - it results in a HTTP ERROR 500 which maybe has something to do with the firewall?!
Console Error: Failed to load resource: the server responded with a status of 500 ()

Please let me know if you see anything wrong with the code and if you don't I will further look into the firewall settings (which doesn't seem to have generated any report on this)

The code that I have tried:
/*
add_action( 'toolset_association_created', 'toolset_number_child_posts', 10, 5 );
add_action( 'toolset_before_association_delete', 'toolset_number_child_posts', 10, 5 );

function toolset_number_child_posts( $relationship_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
if( get_post_type($parent_id)=='ep' ) {
$entity_portfolio_args = array(
'numberposts' => 1,
'post_type' => 'entity_portfolio',
'orderby' => 'post_date',
'order' => 'DESC',
'toolset_relationships' => array(
'role' => 'child',
'related_to' => $parent_id,
'relationship' => 'ep-portfolio-rel'
),
);
$entity_portfolios_query = new WP_Query( $entity_portfolio_args );
$entity_portfolios = $entity_portfolios_query->posts;

if(isset($entity_portfolios[0]) {
update_post_meta( $parent_id, 'wpcf-has-portfolio', 1);
}else {
delete_post_meta( $parent_id, 'wpcf-has-portfolio');
}
}
}

function toolset_number_child_portfolio_on_save( $post_id ) {
if( get_post_type( $post_id ) == 'ep' ) {
toolset_number_child_posts( 'ep-portfolio-rel', $post_id, null, null, null );
}
}
add_action( 'save_post', 'toolset_number_child_portfolio_on_save', 1000);
*/

#1207428

Also, is there a plugin able to extend the toolset filtering & searching capabilities that you would recommend?

#1207818

I can see there is a missing closing parenthesis in this line:

if(isset($entity_portfolios[0]) {

It's easy to miss. The code probably should be:

if(isset($entity_portfolios[0])) {

The only plugin we officially recommend for extending Toolset search is Relevanssi, which can help you search for text in custom fields as well as post title and post body. It won't help you search by related post existence, though. I'm not aware of any plugins that are capable of adding that feature.

#1208191

The issue was with the forgotten bracket you pointed out and I've missed! Thank you!