1

I'm trying to improve this tutorial script including a switch in the first ifcondition to assign a custom priority to my Yoast Xml Sitemap only to certain post or pages.

So i'm trying to say to Yoast which if some pages have certain id the priority have to change, but my script assign the 0.5 value to all pages.

Any help?

add_filter( 'wpseo_xml_sitemap_post_priority', 'my_custom_post_xml_priority', 10, 3 );

function my_custom_post_xml_priority( $return, $type, $post) {

if($type == 'page') {
switch ($post) {
case '8':
case '395':
case '342':
$return = 0.9;
case '5':
$return = 1.00;
case '620':
case '703':
case '603':
case '688':
case '695':
case '614':
case '684':
case '639':
case '628':
case '539':
case '542':
case '521':
case '509':
case '517':
case '528':
case '533':
case '536':
case '548':
case '545':
case '525':
case '551':
$return = 0.7;
break;
default:
$return = 0.5;
break;
}
}

elseif ($type == 'post') {
$return = 0.8;
}

return $return;
}
  • Is $post really a number formatted as a string? – fuxia Apr 10 '16 at 13:10
  • Following the tutorial it seems to use $post to get the post id. – Emanuele Apr 10 '16 at 13:18
  • If you see the ', 10, 3 ); it means the 3rd $post parameter it's a post object so i'm be able to use $post -> ID – Emanuele Apr 10 '16 at 13:25
1

The solution should is this:

add_filter( 'wpseo_xml_sitemap_post_priority', 'my_custom_post_xml_priority', 10, 3 );

function my_custom_post_xml_priority( $return, $type, $post) {

    if($type == 'page') {
        switch ($post->ID) {
        case '8':
        case '395':
        case '342':
            $return = 0.9;
            break;
        case '5':
            $return = 1.00;
            break;
        case '620':
        case '703':
        case '603':
        case '688':
        case '695':
        case '614':
        case '684':
        case '639':
        case '628':
        case '539':
        case '542':
        case '521':
        case '509':
        case '517':
        case '528':
        case '533':
        case '536':
        case '548':
        case '545':
        case '525':
        case '551':
            $return = 0.7;
            break;
        default:
            $return = 0.5;
            break;
        }
    } elseif ($type == 'post') {
        $return = 0.8;
    }

    return $return;
}

I think that $post is an WP_Post object, then you will have to compare the property ID on the switch.

  • Nice! Perfect solution! – Emanuele Apr 11 '16 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.