• Hey there,

    I’m trying to retrieve focus keyword from a draft post using wordpress API.

    I manage to get the description, title and content but I don’t knwo why i cannot get the yoast focus Keyword.

    Do you have any idea of what I can do ?

    here is a sample of the script I’m using :

    from wordpress_xmlrpc import Client, WordPressPost
    from wordpress_xmlrpc.methods.posts import GetPost
    
    # website connexion
    url = ''
    username = ''
    password = ''
    
    # client
    client = Client(url, username, password)
    
    # Article ID
    post_id = 12345
    
    # data
    post = client.call(GetPost(post_id))
    print(post)
    
    # meta
    yoast_title = None
    yoast_description = None
    yoast_keywords = None
    
    for meta_field in post.custom_fields:
        if meta_field.keys == '_yoast_wpseo_title':
            yoast_title = meta_field.value
        elif meta_field.keys == '_yoast_wpseo_metadesc':
            yoast_description = meta_field.value
        elif meta_field.keys == '_yoast_wpseo_focuskw':
            yoast_keywords = meta_field.value
    
    # display
    print("Titre Yoast:", yoast_title)
    print("Description Yoast:", yoast_description)
    print("Mots-clés Yoast:", yoast_keywords)
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It could be the Yoast meta data is not registered with the “show_in_rest” parameter being true. Without it, the data is not available through the API.

    I’m guessing though, I don’t know Yoast that well. To confirm, I recommend asking in Yoast’s dedicated support forum.

    Thread Starter chris627

    (@chris627)

    Moderator bcworkz

    (@bcworkz)

    Same concept, yes, except applied to post meta data. The linked page doesn’t address post meta specifically, but just as you can use ‘register_taxonomy_args’ to expose taxonomy terms to the API, you could use ‘register_meta_args’ to do the same for meta data. However, unlike taxonomies and post types, we’re not required to register post meta, so Yoast may not be doing so. If not, the filter would not be applied.

    If Yoast doesn’t register their meta data, I think you may be able to do it for them by calling register_meta() yourself. I suspect you can re-register even if they did register for themselves. What will matter is whose code executed last prior to actually using the data.

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

The topic ‘Get focus keyword through WordPress API for a post’ is closed to new replies.