admin管理员组

文章数量:1298180

I need to WP_Query for posts which:

  • either have a postmeta 'answer' set to 'yes', and whatever post_content;
  • or have 'answer' set to 'no' but only if post_content is empty.

So in an ideal world where performance is not an issue and where you can query both for a postmeta and post_content, the args would look something like this:

$args = array(
   'relation' => 'OR',
   'meta_query' => array(
      'key' => 'answer',
      'compare' => '=',
      'value' => 'yes'
   ),
   array(
      'relation' => 'AND',
      'meta_query' => array(
         'key' => 'answer',
         'compare' => '=',
         'value' => 'no'
      ),
      array(
         'key' => 'post_content',
         'compare' => '=',
         'value' => ''
      )
   )
);

Now, we're not in that ideal world and as far as I know I can't check the post_content in the WP_Query in the first place, so my answer is: which way is better to assign a post_content related WP_Query-able value to a post, performance wise?

  1. to programmatically add or remove a postmeta like 'has_content' set to 1 or 0 to the post each time it's created/updated, so I can just do a meta_query;
  2. to programmatically add or remove a tag like 'has_content' set to 1 or 0 to the post each time it's created/updated (and check for it with tag__in in WP_Query);
  3. to programmatically add or remove a custom hidden taxonomy like 'has_content' set to 1 or 0 to the post each time it's created/updated (and check for it with a tax_query in WP_Query);
  4. none of the above.

I tend to exclude point 1 because I've read about meta_query's performance issues, but I am not sure about 2-3-4.

Thanks a lot if you can help!

I need to WP_Query for posts which:

  • either have a postmeta 'answer' set to 'yes', and whatever post_content;
  • or have 'answer' set to 'no' but only if post_content is empty.

So in an ideal world where performance is not an issue and where you can query both for a postmeta and post_content, the args would look something like this:

$args = array(
   'relation' => 'OR',
   'meta_query' => array(
      'key' => 'answer',
      'compare' => '=',
      'value' => 'yes'
   ),
   array(
      'relation' => 'AND',
      'meta_query' => array(
         'key' => 'answer',
         'compare' => '=',
         'value' => 'no'
      ),
      array(
         'key' => 'post_content',
         'compare' => '=',
         'value' => ''
      )
   )
);

Now, we're not in that ideal world and as far as I know I can't check the post_content in the WP_Query in the first place, so my answer is: which way is better to assign a post_content related WP_Query-able value to a post, performance wise?

  1. to programmatically add or remove a postmeta like 'has_content' set to 1 or 0 to the post each time it's created/updated, so I can just do a meta_query;
  2. to programmatically add or remove a tag like 'has_content' set to 1 or 0 to the post each time it's created/updated (and check for it with tag__in in WP_Query);
  3. to programmatically add or remove a custom hidden taxonomy like 'has_content' set to 1 or 0 to the post each time it's created/updated (and check for it with a tax_query in WP_Query);
  4. none of the above.

I tend to exclude point 1 because I've read about meta_query's performance issues, but I am not sure about 2-3-4.

Thanks a lot if you can help!

Share Improve this question asked Oct 13, 2021 at 15:38 MarksMarks 312 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Why would you need tag__in for option 2? You're not searching for multiple values, only your 1 or 0. In which case, options 2 and 3 amount to the same thing behind the scenes: a taxonomy query. This way would likely be the most performant, but that said, if you don't want to mess the taxonomies, you can check for empty post content with a filter.

本文标签: wp queryWhich metaquery and postcontent blend is better in WPQueryperformance wise