Unable to Set rank_math_description Meta Data via WordPress REST API

#640266
  • Resolved Julien Dubois
    Rank Math free

    Hello,

    I’ve been working on programmatically creating posts on my WordPress site using the REST API. Specifically, I’ve been trying to set the rank_math_description meta data (from the Rank Math SEO plugin) while creating a post.

    Here’s a brief overview of the steps I’ve taken:

    I use the endpoint https://MY_WEBSITE_DOMAIN/wp-json/wp/v2/posts to create a post.
    When sending the POST request, I include the following meta data in the payload:
    json
    Copy code
    “meta”: {
    “rank_math_description”: “Test”
    }
    The post gets created successfully, but when I check it, the Rank Math meta description is not set.
    For context, this is part of an automated script that generates content and then pushes it to WordPress. The script has been mostly successful except for this particular issue with setting the Rank Math meta description.

    I’ve also referenced a previous support conversation where a method was suggested to set the rank_math_description. I’ve followed the suggested method, but it doesn’t seem to work in my context.
    https://support.rankmath.com/ticket/how-to-setup-meta-description-while-creating-post-using-wordpress-rest-api/

    I’m reaching out to see if there are any known issues with this or if there’s a specific method or endpoint I should be using to set the rank_math_description meta data while creating a post. Any guidance or alternative solutions would be greatly appreciated.

    Thank you for your time and assistance.

    Best regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • what it the end point for for update metata?
    this
    exemple.com/wp-json/rankmath/v1/updateMeta/ id of post

    don’t work for me,)

    I found a way top have my meta by api,)
    Not sure if it is the best way but it’s work

    i have to add this in my functions.php

    // Cette action ajoute le champ personnalisé à l'API REST
    add_action( 'rest_api_init', 'add_custom_fields' );
    
    function add_custom_fields() {
        register_rest_field(
            'post', 
            'rank_math_description', // Nom du champ dans la réponse JSON
            array(
                'get_callback'    => 'get_rank_math_description_field',
                'update_callback' => 'update_rank_math_description_field',
                'schema'          => null,
            )
        );
    }
    
    // Cette fonction récupère la valeur du champ personnalisé pour l'ajouter à la réponse JSON
    function get_rank_math_description_field( $object, $field_name, $request ) {
        return get_post_meta( $object['id'], $field_name, true );
    }
    
    // Cette fonction met à jour la valeur du champ personnalisé
    function update_rank_math_description_field( $value, $object, $field_name ) {
        return update_post_meta( $object->ID, $field_name, strip_tags( $value ) );
    }

    Hello,

    Thank you for your query and we are so sorry about the trouble this must have caused.

    The code in the last message you sent is the only way to get the postmeta from our plugin to update via the REST API. We don’t have any custom routes and the metadata is also not registered by default so registering the metadata and updating via the default functions is the only way.

    Hope this helps clarify the situation.

    Don’t hesitate to get in touch if you have any other questions.

    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 ‘Unable to Set rank_math_description Meta Data via WordPress REST API’ is closed to new replies.