• 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 ) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix.'_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?

Viewing 10 replies - 1 through 10 (of 10 total)
  • According to the source code of WordPress wp_transition_post_status() which handles the transitions of post statuses, the first argument passed to the callback function (in your case on_jobs_publish) is post $ID, not $post. This way you could edit your function to use that $ID in the insert query instead of $post->id.

    Thread Starter packjazz

    (@packjazz)

    I have tried using ID like this:

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

    But it didn’t work. It didn’t even add a new record into the DB. At least with $post it added a new record…the only problem is the record’s post_id was 0.

    Coding aside, I use this plugin as well, and when I go to the settings page SEO > XML Sitemaps, it has an option to exclude post types. In other words, custom post types should automatically be included in the sitemap unless otherwise excluded.

    Thread Starter packjazz

    (@packjazz)

    @ryan – Right. They should. And some of them are, but not all of them. There’s no rhyme or reason as to why some custom posts do get added to the sitemap and some don’t. We’ve spent quite a lot of time trying to figure out why, but we have gotten nowhere. The only way to ensure that posts get added to the sitemap is to select the option to force it on to the sitemap. My client wants this to happen automatically, hence the need for this function to work. It should work according to the codex documentation, but it isn’t producing a post ID.

    Can you confirm, in the file that registers your custom post type, that it is exactly called custom-posts (with the ‘s’ and the dash)?

    And then try this simplified version:

    add_action('publish_custom-jobs', 'publish_custom_jobs');
    
    function publish_custom_jobs($post_id) {
        update_post_meta(
        	$post_id,
        	'_yoast_wpseo_sitemap-include',
        	'always'
        );
    }
    Thread Starter packjazz

    (@packjazz)

    @ryan – Yes, I’ve confirmed that I’m using the right name for the custom post type, dash and ‘s’ included. And we know what I have is at least partially correct because it does infact create a new DB record any time I publish a custom post of that type. The one missing thing is that the post_id is recorded as 0 instead of the actual id of the post that was published.

    I tried that simplified version (which I had tried before in a slightly different configuration) and it doesn’t work. It doesn’t even add a new record to the DB.

    I’m starting to feel like this is an issue with how the theme is configured (it’s a complicated membership site set-up that I wasn’t involved in). I’m just having a hard time even knowing where to begin to look to figure it out.

    Thread Starter packjazz

    (@packjazz)

    Also note I have disabled all plugins as part of troulbeshooting and that didn’t help. I just need to figure out why the function isn’t passing the post id properly.

    I installed a clean version of WordPress, put the code I sent in functions.php, registered a dummy post type called custom-jobs, and it did work, so we know it’s something unique to your installation.

    Can you temporarily change your theme to a default WordPress theme and place the code above in the theme’s functions.php? Or is the post type registered in the theme?

    Thread Starter packjazz

    (@packjazz)

    Yeah, the post type is registered in the theme. But thanks for verifying that the code is right, seems it’s just something funky going on with the current theme. I’ll see if I can find another way to accomplish what I’m looking to accomplish. Thanks for your help!

    You might give the save_post hook a shot, which fires every time any post is saved. The first argument is $post_id.

    add_action('save_post', 'my_save_post');
    
    function my_save_post($post_id) {
        update_post_meta(
        	$post_id,
        	'_yoast_wpseo_sitemap-include',
        	'always'
        );
    }

    If that works, then you just have to add a bit more code so that this only runs when desired.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Add meta value to custom post on publish’ is closed to new replies.