最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the diference between Post Metadata and Queried Object

programmeradmin0浏览0评论

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 ?

Share Improve this question asked Oct 7, 2020 at 11:05 iamonstageiamonstage 1671 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );
发布评论

评论列表(0)

  1. 暂无评论