Tell us what you are trying to do?
I have everything set for a alphabetic filter. I have a taxonomy alphabet with all the letters, the only thing I need is when a post is saved, the first letter of the title is recognized and add to the correct letter in the alphabet taxonomy. There is a guy who has found a tutorial for this, but the links hidden:
https://toolset.com/forums/topic/how-to-include-an-alphabetical-filter-into-a-custom-search/
Do you have the link or maybe a function that I can add?
Hi, here's the link: https://cybmeta.com/como-crear-un-glosario-o-listado-alfabetico-de-posts
I also removed the restriction from the original post so the link is visible for everyone now. Good luck with your project!
Hi Christian,
Thanks so much!
I've tried the function, but no luck..could it be that Toolset or WordPress has changed and this function needs to be modified to get it working? I changed 'term' to custom post type slug and 'glossary' to custom taxonomy slug and nothing..
add_action('save_post','cyb_set_first_letter');
function cyb_set_first_letter( $post_id ){
// skip autosave
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
// limit to term post type
if( isset($_POST['post_type']) && 'term' != $_POST['post_type'] ) {
return;
}
// Check permissions
if( !current_user_can('edit_post', $post_id ) ) {
return;
}
//Assign first letter of the title as glossary term
if( isset($_POST['post_title']) ) {
//TODO: skip no-sense words, characters, etc
$glossary_term = mb_strtolower( mb_substr( $_POST['post_title'], 0, 1 ) );
wp_set_post_terms(
$post_id,
$glossary_term,
'glossary'
);
}
//delete the transient that is storing the alphabet letters
delete_transient('cyb_archive_alphabet');
}
- What is the slug of your custom post type?
- What is the slug of your taxonomy?
- How are these posts being created - in Forms, or in wp-admin?
- What is the slug of your custom post type?
flowers
- What is the slug of your taxonomy?
alphabet
- How are these posts being created - in Forms, or in wp-admin?
in wp-admin
Just in case, I placed the custom post type slug and slug of taxonomy in the code and it didn't work if that was your question.
Try this code:
add_action('save_post','cyb_set_first_letter');
function cyb_set_first_letter( $post_id ){
// skip autosave
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
// limit to term post type
if( isset($_POST['post_type']) && 'flowers' != $_POST['post_type'] ) {
return;
}
// Check permissions
if( !current_user_can('edit_post', $post_id ) ) {
return;
}
//Assign first letter of the title as alphabet term
if( isset($_POST['post_title']) ) {
//TODO: skip no-sense words, characters, etc
$alphabet_term = mb_strtolower( mb_substr( $_POST['post_title'], 0, 1 ) );
wp_set_post_terms(
$post_id,
$alphabet_term,
'alphabet'
);
}
}
If this doesn't work, I'll need to log in and take a closer look. Please provide login credentials in the private reply fields here.
Okay the Alphabet taxonomy was listed as "hierarchical" instead of "flat", and I didn't realize that made such a difference. The two types of taxonomies are managed a bit differently by the wp_set_post_terms function, as hierarchical taxonomies require a numeric ID instead of a term slug. I checked the terms and everything seems to be flat, so I made the adjustment from hierarchical to flat in the taxonomy editor. Now it appears the terms are being assigned as expected. Can you confirm? And please let me know if the taxonomy needs to be hierarchical for any reason.
It doesn't need to be hierarchical, so thanks so much Christian!!!!
My issue is resolved now. Thank you!