Rank Math not getting all the Custom Fields

#39823
Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael Davis
    Rank Math pro

    Hello,

    Thank you for contacting Rank Math today.

    I am sorry but this is the default working of this variable. It will return the first selected term on the custom taxonomy.

    To be able to get all the terms, you will need to register a custom variable name using the following filter:

    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
     */add_action( 'rank_math/vars/register_extra_replacements', function(){
     rank_math_register_var_replacement(
     'custom_variable_slug',
     [
     'name'        => esc_html__( 'Custom variable name.', 'rank-math' ),
     'description' => esc_html__( 'Custom variable description.', 'rank-math' ),
     'variable'    => 'custom_variable_slug',
     'example'     => 'custom_variable_callback()',
     ],
     'custom_variable_callback()'
     );
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Oscar Sanchez
    Rank Math pro

    Hi,

    Thank you very much. Unfortunately, I don’t know how to use the filter and grab all the “plataformas” custom field.

    Can you help us a little bit to make this change?

    Best regards.

    Michael Davis
    Rank Math pro

    Hi Oscar,

    Thanks for the reply.

    Here is some code that you can modify to register a new variable called %All_Categories% :

    
    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
    * Snippet to register variable that will return all terms in custom taxonomy
     */
    
    add_action( 'rank_math/vars/register_extra_replacements', function(){
     rank_math_register_var_replacement(
     'All_Categories',
     [
     'name'        => esc_html__( 'All categories', 'rank-math' ),
     'description' => esc_html__( 'Returns all product categories', 'rank-math' ),
     'variable'    => 'All_Categories',
     'example'     => my_variable_callback(),
     ],
     'my_variable_callback'
     );
    
    });
    function my_variable_callback(){
    	global $post;
            //Replace 'categorias-videojuegos' with the name of taxonomy if different
    	$taxonomy = 'categorias-videojuegos';
            // only call this code on specific post type
            // Replace post_type_name with the name of your post type
        if(is_singular('post_type_name')){
        // Get the term IDs assigned to post.
        $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     
        // Separator between links.
        $separator = ', ';
     
        if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
     
        $term_ids = implode( ',' , $post_terms );
     
        $terms = wp_list_categories( array(
            'title_li' => '',
            'style'    => 'none',
            'echo'     => false,
            'taxonomy' => $taxonomy,
            'include'  => $term_ids
        ) );
     
        $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
     
        // Display post categories.
        return  $terms;
    	}
    }
    }

    I have added some comments on the areas that you need to modify to customize for your post type.

    I hope this info helps. Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Rank Math not getting all the Custom Fields’ is closed to new replies.