What is the diference between Post Metadata and Queried Object

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 ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Q1 – 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:

  1. Post metadata are saved in the wp_postmeta table. (Note though, the table prefix might be different in your case, but wp_ is the default.)
  2. The queried object in question refers to the post object/data which is retrieved from the wp_posts table, which contains fields like ID, post_type and comment_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 to open ?

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 );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x