Skip Navigation

[Resolved] Filtering child results by parent post field using radio button

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem: I have a View of child posts that I would like to filter by the value of a custom field on their parent post.

Solution: There's not a good way to filter based on parent post custom fields, so it's necessary to automatically set a custom field on the child post to match the field on its parent. You can use the following code:

function copy_parent_gender_field ( $post_id ) {
  // when parent narrator post type is saved, copy its gender field to child audio posts
  $post_type = get_post_type( $post_id );
  if ( $post_type == 'narrator' ) {
    $gender = get_post_meta( $post_id, 'wpcf-gender', true );
    $audio_args = array(
      'numberposts'        => -1,
      'post_type'          => 'audio-sample',
      'meta_query'         => array(
        'relation'         => 'AND',
        array(
          'key'              => '_wpcf_belongs_narrator_id',
          'value'            => $post_id,
          'compare'          => '=',
          'type'             => 'NUMERIC'
        )
      )
    );
    $audios = get_posts( $audio_args );
    foreach( $audios as $audio ) {
      update_post_meta( $audio->ID, 'wpcf-gender', $gender );
    }
  }
}
add_action( 'save_post', 'copy_parent_gender_field', 100 );

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Function_Reference/update_post_meta

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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 17 replies, has 2 voices.

Last updated by taih 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#571165

I have created a radio filter for the page hidden link

I am trying to put another radio button filter next to the Fiction/ Nonfiction radio, but for Gender.
I tried creating it the same way as the nonfiction filter, but its not working and I am missing something.

Heres what I did for the non fiction that works:

Query Filter :
Fiction/Non-Fiction is a string equal to URL_PARAM(wpv-wpcf-fiction_nonfiction)

Filter Editor :
<div class="form-group">
<h2><label>[wpml-string context="wpv-views"]Fiction/Non-Fiction[/wpml-string]</label></h2>
[wpv-control-postmeta field="wpcf-fiction_nonfiction" url_param="wpv-wpcf-fiction_nonfiction"]
</div>

That works great, but the gender field isnt part of the "Audio Sample" post type like the non-fiction is. The "Gender" field is part of the "Narrator" post type, which is the parent of the "Audio Sample" post. Is this causing th eproblem? Should I amend it with a id=narrator like I did in the loop?

Here's the loop for the results:

<h6><span class="classification">Classification: </span>[types field='fiction_nonfiction' id='$sample'][/types]</h6>

<h6><span class="classification">Genre: </span>[wpv-post-taxonomy type="genre"]</h6>
<h6><span class="classification">Accents: </span>[wpv-post-taxonomy type="dialects"]</h6>
<h6><span class="classification">Gender: </span>[types field='gender' id='$narrator'][/types]</h6>

#571167

Yes, you've guessed the problem. It's not possible to filter a custom search of post type A based on a post field in related post type B. I think the only option would be to create the same custom field on the audio sample post type, and populate it automatically based on the value set in the parent post. Then you could filter using this value. I can probably help you implement some custom code that will handle this. Are these posts created and edited by CRED, or managed manually in wp-admin?

#571171

That sounds like an interesting approach that would help with other issues as well. I have already created a "Narrator Name" field in the audio samples post type to make the name search work too. It sounds like the same idea.

I am open to any solution that can filter the genders, although the template I am using has the edit turned off for functions.php

#571174

Can you use a custom code plugin like Code Snippets?
https://wordpress.org/plugins/code-snippets/

This will allow you to include custom code without modifying functions.php.

#571175

Yes. I have installed the code snippet plugin

#571180

I just noticed that I didn't answer your question:
All the posting is done in wp admin, not cred.

#571181

Okay thanks, I'm working on a snippet for you now, standby.

#571184

Please activate the Audio Sample post type for the gender field group, then add this snippet using your code snippet plugin:

function copy_parent_gender_field ( $post_id ) {
  // when parent narrator post type is saved, copy its gender field to child audio posts
  $post_type = get_post_type( $post_id );
  if ( $post_type == 'narrator' ) {
    $gender = get_post_meta( $post_id, 'wpcf-gender', true );
    $audio_args = array(
      'numberposts'        => -1,
      'post_type'          => 'audio-sample',
      'meta_query'         => array(
        'relation'         => 'AND',
        array(
          'key'              => '_wpcf_belongs_narrator_id',
          'value'            => $post_id,
          'compare'          => '=',
          'type'             => 'NUMERIC'
        )
      )
    );
    $audios = get_posts( $audio_args );
    foreach( $audios as $audio ) {
      update_post_meta( $audio->ID, 'wpcf-gender', $gender );
    }
  }
}
add_action( 'save_post', 'copy_parent_gender_field', 100 );

This code assumes your post type slugs are "narrator" and "audio-sample", and your field slug is "gender". If that's not correct, you must modify the code to match your slugs.

Now any time a parent Narrator post is saved, all its child Audio Samples will be updated to include the proper gender value. If Audio Samples are created independently, but you don't set the gender value in the Audio Sample, it will not contain the proper value until the parent Narrator post is saved again.

#571186

Can you clarify this:
Please activate the Audio Sample post type for the gender field group

Should I create a new field in the audio sample post type for Gender?

#571188
Screen Shot 2017-09-18 at 4.40.46 PM.png

I assumed you would use the same field, so you would need to add "Audio Samples" post type in the Post Field Group that contains this field. See the attached screenshot.

If that's not the case, and you want to create a separate field, that's fine. You must then choose a different slug for the Audio Samples gender field, since you cannot use identical slugs. Then you must update the code above on line 21 to reflect the new slug. The prefix "wpcf-" is required in the code, but you should not add it to your post slug in the field editor. Make sense?

#571192

Yes, it makes total sense. The only issue I have is that the Narrator field group also has fields for pic, bio as well as gender. I dont want those to be in the admin for the audio samples. It will just confuse the website managers. Then I should probably create a new field with unique slug?

#571193

Yes, in that case a separate field makes the most sense. Let me know if you run into issues with the code.

#571196

First of all, thank you so far!
I follow whats supposed to happen and I've double checked but it doesnt seem to be copying the narrator gender field value to the audio sample field value on save.

Post types:
Narrators
Samples

Heres what I did:
Create field in samples field group: Gender , gender-audiosample

Add this to snippets:
function copy_parent_gender_field ( $post_id ) {
// when parent narrator post type is saved, copy its gender field to child audio posts
$post_type = get_post_type( $post_id );
if ( $post_type == 'narrators' ) {
$gender = get_post_meta( $post_id, 'wpcf-gender', true );
$audio_args = array(
'numberposts' => -1,
'post_type' => 'samples',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_wpcf_belongs_narrators_id',
'value' => $post_id,
'compare' => '=',
'type' => 'NUMERIC'
)
)
);
$audios = get_posts( $audio_args );
foreach( $audios as $audio ) {
update_post_meta( $audio->ID, 'wpcf-gender-audiosample', $gender );
}
}
}
add_action( 'save_post', 'copy_parent_gender_field', 100 );

#571197

Looks good to me, can I log in to your wp-admin area to see what's going wrong? If that's okay, please provide login credentials in the private reply fields below.

#571202

Check your post type slugs. The Narrators post type slug is actually "narrator" not "narrators". See here:
hidden link
You'll need to modify the code to use "narrator" instead of "narrators". I didn't check "samples" so please double check that one as well. The gender field slugs look good.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.