skip to Main Content

I’m trying to insert meta description for my WordPress post, but the meta-description is always empty.

This is my code:

post_data = {
        'title': title,
        'content':  markdown.markdown(content),
        'slug': slug,
        'status': 'publish',
        'yoast_meta' : {
            'yoast_wpseo_metadesc' : meta_description
         },
        'author': author_id,
        'categories': [category_id],  # Assign the category ID to the post
        'meta': {
            'meta_description': meta_description,
        },
    }

I’m not sure if the field name is the right one or if Yoast even supports feeding the meta description from API.

2

Answers


  1. You need to use the _yoast_wpseo_metadesc key instead of yoast_wpseo_metadesc, as this is the actual key used by Yoast SEO to store the meta description.

    post_data = {
        'title': title,
        'content': markdown.markdown(content),
        'slug': slug,
        'status': 'publish',
        'meta': {
            '_yoast_wpseo_metadesc': meta_description
        },
        'author': author_id,
        'categories': [category_id],
    }
    
    Login or Signup to reply.
  2. Had the same issue. If you install the following plugin: https://github.com/ChazUK/wp-api-yoast-meta

    You can do the following:

    try {
      const response = await wp.posts().create({
        title: "Your Post Title",
        content: "Your post content",
        status: "publish",
        yoast_meta: {
          yoast_wpseo_title: "Your SEO title",
          yoast_wpseo_metadesc: "Your SEO description",
        },
      });
      console.log(response.id);
    } catch (error) {
      console.error(error);
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search