0

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!

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.

1
  • You're right pointing out about options 2 and 3, I'll probably go with a tax_query. My concern about the WHERE filter you mention is that it affects the whole query, whereas I need to apply the post_content clause (thus the WHERE filter, if I used it) only in 1 out of the 2 scenarios of the meta_query. Correct me if I'm wrong.
    – Marks
    Oct 14 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.