1

I have a client who is requesting that all posts of a certain custom post type (called 'custom-jobs') be set to automatically be included in the sitemap in the Yoast SEO plugin.

I found that in order to do this, the post needs a meta key of '_yoast_wpseo_sitemap-include' with a value of 'always'. So I attempted to call a function using the {status}_{post_type} hook as follows:

function on_jobs_publish( $post_ID ) {
    global $wpdb;
    $wpdb->insert( 
        'iCrewzWp_postmeta', 
        array( 
            'post_id' => $post->ID,
            'meta_key' => '_yoast_wpseo_sitemap-include',
            'meta_value' => 'always'
        ), 
        array( 
            '%d',
            '%s',
            '%s'
        ) 
    );
}
add_action(  'publish_custom-jobs',  'on_jobs_publish', 10, 1 );

This almost works. It adds the meta info to the DB, but the post_id is always set to 0. I've tried passing and using $post and $ID variables as well with no luck. Any idea how I can pass the custom post's id into this function so the meta info is associated with the post that was published?

1

You are usgin an undefined $post->ID variable because there is no reference to any $post object in your code; instead, use the $post_ID variable retrieved in the function:

function on_jobs_publish( $post_ID ) {
   global $wpdb;
   $wpdb->insert( 
       'iCrewzWp_postmeta', 
            array( 
                'post_id' => $post_ID,
                'meta_key' => '_yoast_wpseo_sitemap-include',
                'meta_value' => 'always'
        ), 
        array( 
            '%d',
            '%s',
            '%s'
        ) 
    );
}
add_action(  'publish_custom-jobs',  'on_jobs_publish', 10, 1 );

Also, instead of working with the global $wp_query and run the insert method, I think it would be better to user update_post_meta function.

function on_jobs_publish( $post_ID ) {
    update_post_meta($post_ID, '_yoast_wpseo_sitemap-include', 'always' );
}
add_action(  'publish_custom-jobs',  'on_jobs_publish', 10, 1 );

Note that publish_{post-type} action is triggered when the post change the status from any value to "publish". This mean that if the post is already published and you try to update it, this function won't be executed. If you need to execute it without taking care of the post status, use save_post action hook:

function on_jobs_publish( $post_ID, $post ) {
    //Check that the post type being edited is our custom post type
    if ( $post->post_type != 'custom-jobs' ) {
        return;
    }
    update_post_meta($post_ID, '_yoast_wpseo_sitemap-include', 'always' );
}
add_action(  'save_post',  'on_jobs_publish', 10, 2 );

And one more advise for future. When you use wpdb class, like you are using insert method, never use the full table name, instead replace the prefix with $wpdb->prefix property. It is much safer. For example, in your code:

   global $wpdb;
   $wpdb->insert( 
       $wpdb->prefix.'_postmeta',
   //Rest of the code
  • Thanks cybmeta. That was a typo in the original post, the $post_ID in the first line should have been $post. When I do it that way, it at least creates a new row in the DB, but it has 0 for the post ID. When I try using $post_ID it doesn't even create a new DB record. I had tried using the update_post_meta function before, but it doesn't seem to work. Any other ideas? – MattR Dec 29 '14 at 5:10
  • Please, use the exact code provide in the answer. You had not a typo. You are missunderstanding what the parameter of the function is. The parameter of the function contains the post ID, not a post object, so inside the function you can not use something like $post->ID as you did. It is better if you leave the first line with $post_ID because it represent perfectly what that variable is. Then, inside the function you have to use $post_ID (not $post->ID, exactly $post_ID) wherever you need access to the post ID. If you do that, you can use update_post_meta also. – cybmeta Dec 29 '14 at 8:20
  • Yeah, I understood. What I was saying was my original post had a typo, I should have written $post in the first line as the parameter I was passing. I did try your exact code, passing the parameter of $post_ID and calling $post_ID in the array, and it doesn't work. I'm beginning to think this is an issue with this theme, because for all intents and purposes this should be working. – MattR Dec 30 '14 at 15:54
  • I tested the code and it works perfectly (tested only the update_post_meta version because it is the correct one from my point of view). Can you verify that custom-jobs is the correct identifier of the custom post type? Also, note the publish_{post-type} only will be triggered when the post comes from any other status to "publish". And I don't want to continue the discussion but if you had written $post in the first line, as you say, you was still using it as an object ($post->ID) when it is not; that why I say it is not a typo but a missunderstanding of what that variable is. – cybmeta Dec 30 '14 at 16:00
  • Gotcha. Yeah, it's still not working. Everything is verified. It's gotta be something in this theme that's messed up. It's a custom theme built for a membership site that I had no involvement in, it's pretty bloated so I'm sure there's something in there that's causing this not to work. Thanks for your help, I'm going to mark your answer as correct because it SHOULD work ;-). – MattR Dec 30 '14 at 16:10
0

I don't think you should manually update the wpdb or the post meta but search for the right settings in the plug-in.

Maybe Yoast Seo can't see your custom post type because 'public' => true isn't set when cpt is created?

  • Hey brobken, there's really no setting in the plugin to force all posts of a certain type to be added to the sitemap by default. I even contacted Yoast support to ask them and they weren't able to help. I'd like to avoid modifying the plugin files directly which is why I'm trying to go this route of modifying the DB directly. I've verified the custom post type is set to public. The plugin can see the post type because it lists it and creates a sitemap for it....it just isn't listing every post that gets published in the sitemap. – MattR Dec 29 '14 at 5:15
  • I got your point. But I think you should research how it's possible that not every post is getting listed in the sitemap. Because when in Yoast seo settings, in the types tab, meta robots: noindex, follow is unchecked en you leave alone the automatic settings in the post itself... They should be included every time. Or am I wrong? – brobken Dec 29 '14 at 23:55
  • 1
    Yeah, I think that's the way it's supposed to behave, but it isn't. I've spent hours trying to figure out why, but to no avail. I'm going to look into maybe running a chron job once or twice a day that will just run a SQL script to add the meta info to any existing posts. I just need this to work, even if it's an in-elegant solution. Time and money constraints aren't allowing me to dig into this as much as I could. Thanks though. – MattR Dec 30 '14 at 15:56

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.