skip to Main Content

Since there is no documentation about this, I was wondering in what way it is possible to get filtered Yoast WP SEO data (so not unfiltered _yoast_wpseo metadata) by given post ID.

Ideally, I would like to get the exact representation of the HTML output by the Yoast plugin, or a full set of data in a JSON object. Ideas?

2

Answers


  1. Untested idea: most of it runs on wpseo_head (hooked into wp_head). Turn on output buffering, make sure your postdata is setup up, and execute it.

    ob_start();
    do_action("wpseo_head");
    $yoast = ob_get_contents();
    ob_end_clean();
    
    Login or Signup to reply.
  2. Use
    get_post_meta($postId, '_yoast_wpseo_focuskw') and get_post_meta($postId, '_yoast_wpseo_metadesc')

    I found this question trying to get yoast data from a particular post (by ID) and came to this solution:

    Made a function that recieves the post id

    public static function postMetas($postId)
    {
            $metas['keyword'] = get_post_meta($postId, '_yoast_wpseo_focuskw');
            $metas['description'] = get_post_meta($postId, '_yoast_wpseo_metadesc');
    
            return $metas;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search