mardi 30 décembre 2014

Add meta value to custom post type 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_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?





Aucun commentaire:

Enregistrer un commentaire