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>
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?
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
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.
Yes. I have installed the code snippet plugin
I just noticed that I didn't answer your question:
All the posting is done in wp admin, not cred.
Okay thanks, I'm working on a snippet for you now, standby.
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.
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?
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?
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?
Yes, in that case a separate field makes the most sense. Let me know if you run into issues with the code.
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 );
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.
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.