When I edit a Submitted Essay post and look at the Post Metadata using the plugin JSM's Show Post Metadata. I cannot see comment_status
.
But when I view the post using the Debug Bar and look at the WP Query > Queried Object I can see comment_status ⇒ open
.
Q1 - Is there a difference between these 2? - Post Metadata and Queried Object.
I am trying to set the default comment status on LearnDash Submitted Essays (post-type: sfwd-essays
) to open
.
My code below adds the Metadata 'comment_status', 'open'
(as an array, which might be wrong) upon the submission, and I can see that the metadata has been added.
add_action('save_post', 'goldlms_sfwd_essays_comment_open', 10, 2);
function goldlms_sfwd_essays_comment_open($postID, $post)
{
if (isset($post->post_type) && $post->post_type == 'sfwd-essays') {
update_post_meta($post->ID, 'comment_status', 'open');
}
}
Metadata result:
comment_status
array ( 0 => 'open', )
But when I view the post the Debug Query is still comment_status ⇒ closed
Q2 - How do I set the default comment status for sfwd-essays
post types to open
?
When I edit a Submitted Essay post and look at the Post Metadata using the plugin JSM's Show Post Metadata. I cannot see comment_status
.
But when I view the post using the Debug Bar and look at the WP Query > Queried Object I can see comment_status ⇒ open
.
Q1 - Is there a difference between these 2? - Post Metadata and Queried Object.
I am trying to set the default comment status on LearnDash Submitted Essays (post-type: sfwd-essays
) to open
.
My code below adds the Metadata 'comment_status', 'open'
(as an array, which might be wrong) upon the submission, and I can see that the metadata has been added.
add_action('save_post', 'goldlms_sfwd_essays_comment_open', 10, 2);
function goldlms_sfwd_essays_comment_open($postID, $post)
{
if (isset($post->post_type) && $post->post_type == 'sfwd-essays') {
update_post_meta($post->ID, 'comment_status', 'open');
}
}
Metadata result:
comment_status
array ( 0 => 'open', )
But when I view the post the Debug Query is still comment_status ⇒ closed
Q2 - How do I set the default comment status for sfwd-essays
post types to open
?
1 Answer
Reset to default 1Q1 - Is there a difference between these 2? - Post Metadata and Queried Object.
Yes.
And I won't go into a detailed explanation (e.g. what a post metadata is), but these should help you for what you're trying to do:
Post metadata are saved in the
wp_postmeta
table. (Note though, the table prefix might be different in your case, butwp_
is the default.)The queried object in question refers to the post object/data which is retrieved from the
wp_posts
table, which contains fields likeID
,post_type
andcomment_status
— the one you're trying to change.
So if you want to set the comment status of a post, there's no need to add a custom metadata to the post. Just use wp_update_post()
.
Q2 - How do I set the default comment status for
sfwd-essays
post types toopen
?
Not sure if there's a specific setting for that during post type registration, but there's a hook you can use: get_default_comment_status
. For example for your post type:
add_filter( 'get_default_comment_status', function ( $status, $post_type ) {
// Set to open if post type = sfwd-essays
return ( 'sfwd-essays' === $post_type ? 'open' : $status );
}, 10, 2 );