wp_set_post_terms issue for alphabetical indexing
-
For our site we would like to make an ABC index page of all our album reviews. Having bought the posts table pro plugin and the pods plugin installed we have followed an online tutorial but are currently stuck.
In short: we want the function wp_set_posts to chop off the first letter of a meta key value and store it.
long read: For example, imagine an article with the following title. “David Bowie – Blackstar”.
The current bit of code we have will chop off the first letter of the title, being a D, and consequently sort the article under D.
Since we want it sorted on lastnames, so B for Bowie, all of our articles already have a keyword attached, in this specific instance Bowie. These keywords are stored in a custom field, created by the advanced custom field plugin, called ‘keywordmuziek’. This meta key can be found in the database in the fa9l_postmeta table in two forms:
Meta key:_keywordmuziek meta value: field_5acf19694741c
and
Meta key: keywordmuziek meta value: Bowie DavidWe are provided with the following code (adjusted according to the tutorial):
/* When the post is saved, saves our custom data */ function kia_save_first_letter( $post_id ) { // verify if this is an auto save routine. // If it is our form has not been submitted, so we dont want to do anything if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id; //check location (only run for posts) $limitPostTypes = array('post'); if (!in_array($_POST['post_type'], $limitPostTypes)) return $post_id; // Check permissions if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id; // OK, we're authenticated: we need to find and save the data $taxonomy = 'alphabetical_letter'; //set term as first letter of post title, lower case wp_set_post_terms( $post_id, strtolower(substr($_POST['keywordmuziek'], 0, 1)), $taxonomy ); //delete the transient that is storing the alphabet letters delete_transient( 'kia_archive_alphabet'); } add_action( 'save_post', 'kia_save_first_letter' );
We are certain that the wp_set_post_terms function needs to be adjusted but we sadly have no clue on how to proceed. If anyone could shed a light on this it would be greatly appreciated. The original tutorial from which we started can be found here: https://barn2.co.uk/wordpress-alphabetical-index/
thanks for your insights in advance.
- The topic ‘wp_set_post_terms issue for alphabetical indexing’ is closed to new replies.