Support » Plugin: The SEO Framework » Get Canonical URL in Rest API

  • Resolved elwwww



    I would like to retrieve the canonical url if set via the SEO Framework on the rest API.

    I tried:

    add_action('rest_api_init', function(){
      register_rest_field('post', 'canonical', 
        'get_callback' => 'get_canonical', 
        'update_callback' => null,
        'schema' => null
    function get_canonical ( $object ) {
        //get the id of the post object array
        //return the post meta
        return  the_seo_framework()->get_canonical_url($object);

    But this does not return the value if I change it on the backend, it only returns the url of the POST.

    • This topic was modified 1 month, 3 weeks ago by elwwww.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Sybre Waaijer



    get_canonical_url()‘s first parameter is marked as private.

    Use create_canonical_url() instead. It’ll build up a query, and it includes a parameter to get user-defined input.

    return the_seo_framework()->create_canonical_url( [
    	'id'               => 42,
    	'taxonomy'         => 'category', // Optional. Leave empty/out for a post-query.
    	'get_custom_field' => true, // this is what you need.
    ] );

    I hope that’ll do the trick 🙂 Cheers!

    Thanks Sybre, I ended up using: get_singular_custom_canonical_url which worked fine.

    Do you also happen to know how to get the 301 redirect?

    Plugin Author Sybre Waaijer


    Hi again!

    The method I described will also fall back to the generated URL when no custom URL can be found. Might you desire that extra functionality 🙂

    For the redirect value, either of these will do, for posts and terms respectively:

    $post_redirect = the_seo_framework()->get_post_meta_item( 'redirect', $post->ID );
    $term_redirect = the_seo_framework()->get_term_meta_item( 'redirect', $term_id );
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.