Add meta value to custom post on publish
-
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)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘Add meta value to custom post on publish’ is closed to new replies.