• I want to return the value of the WPSEO metadesc for several categories on a page in my site can anyone provide me with code to get that data? I can get the WPSEO meta title fine but the metadesc never is returned.

    This all works except for the metadesc:

    add_shortcode( ‘catDisplay’, ‘catDisplay’ );
    function catDisplay(){
    ob_start();

    $prod_categories = get_terms(array(
    ‘taxonomy’ => ‘product_cat’,
    ‘orderby’ => ‘include’,
    //’order’ => ‘ASC’,
    ‘hide_empty’ => 1,
    ‘depth’ => 1,
    ‘include’ => array( 114,104,305,66,29 )
    ));

    echo “<div class=’row’>”;
    foreach( $prod_categories as $prod_cat ){
    $cat_name = $prod_cat->name;
    $cat_id = $prod_cat->term_id;
    $meta = get_post_meta(‘_yoast_wpseo_metadesc’, true);
    $seoTitle = $meta[‘product_cat’][$cat_id][$meta];
    $cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, ‘thumbnail_id’, true ); //establishes category thumbnail ID
    $cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id ); //uses category ID to get category thumbnail
    $term_link = get_term_link( $prod_cat, ‘product_cat’ );
    echo “<div class=’col-md-4 col-sm-4′>".$cat_name."<h4>”.$cat_name.”</h4>“.$seoTitle.”</div>”;
    }
    echo “</div>”;
    wp_reset_query();

    return ob_get_clean();
    }

    • This topic was modified 7 years, 5 months ago by boudga.
Viewing 1 replies (of 1 total)
  • Thread Starter boudga

    (@boudga)

    The meta fields “yoast_wpseo_title” and “yoast_wpseo_metadesc” are considered protected by WordPress. To make an API request for them, you must first remove its protection. For this you can use the filter “is_protected_meta.”

    Since this filter isn’t part of the REST API, it affects any use of these fields. Make sure the protection is only removed during API calls—which can be determined based on the constant REST_REQUEST.

    First, hook the filter “is_protected_meta.” If the REST_REQUEST constant is true, then, in the callback, set these two fields as unprotected.

Viewing 1 replies (of 1 total)
  • The topic ‘query for WPSEO metadesc for several category IDs’ is closed to new replies.