Skip Navigation

[Resolved] Alphabetic order autosave in taxonomy

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

Problem: I would like to create a list of posts sorted by first letter, but the code I found on the site doesn't seem to work.

Solution: Use the following custom code and make sure the taxonomy is listed as "flat", not "hierarchical".

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'
    );
  }
}
This support ticket is created 4 years, 12 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by bartv-9 4 years, 12 months ago.

Assisted by: Christian Cox.

Author
Posts
#1233889

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?

#1233984

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!

#1234137

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');

}

#1234693

- 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?

#1235407

- 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

#1235602

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.

#1235677

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.

#1235788

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.

#1235960

It doesn't need to be hierarchical, so thanks so much Christian!!!!

#1235961

My issue is resolved now. Thank you!

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