• 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 David

    We 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.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    wp_set_post_terms() is for assigning taxonomy terms. Taxonomies like category or tag. It is entirely unrelated to post meta or custom fields. It’s illogical to have this function do what you plan.

    If I understand correctly, you want to list posts alphabetically by a custom field value. Why would you want to “chop off the first letter” of the very value with which you wish to sort posts by? How can posts be properly sorted if the first letter is missing? Since custom fields are saved by WP, I don’t see the need to do anything extra while saving posts.

    You can list posts ordered by meta value by setting the query var “orderby” to “meta_value” and setting the “meta_key” query var to “keywordmuziek” in a callback to “pre_get_posts” action. You will need some conditionals in your callback to ensure the changes are only applied to the desired query and not any other queries.

Viewing 1 replies (of 1 total)
  • The topic ‘wp_set_post_terms issue for alphabetical indexing’ is closed to new replies.