I made some researches for my question, but found nothing.

I have a query that gets all posts from a specific post type and where a specific custom field is not empty. But I would like that this same custom field has a max characters' length. And I don't want to trim the value, I only want to get posts that have a characters' length between 1 and 75.

Here is my current query that works but without the characters' length.

new WP_Query(array(
    'post_type' => 'experts',
    'posts_per_page' => 1,
    'meta_key' => 'quote',
    'meta_value' => '',
    'meta_compare' => '!=',
    'orderby' => 'rand'
  • 2
    Can you provide some context as to why you would want such a query? Meta queries are expensive so you'll need to wrap it in caching code, and random order is extremely expensive and could cripple your database server. I would expect the above query to take 2+ seconds to run on all but the fastest machines without caching – Tom J Nowell Sep 28 '16 at 17:30

No, out of the box there isn't. There are alternatives but the best solutions will require knowledge of your reason for wanting this.

For example, if your goal is to show full quotes on the homepage and avoid a box that's too large and disrupts the design, you could have:

  • A CSS based solution
  • A custom taxonomy with a "show on homepage" tag ( protip: this would be orders of magnitude faster than your meta query )
  • A tag to indicate if the quote falls into a certain character length range, which can then be picked up in your query

However without that information, the only optimal answer is no, WP_Query can't do string manipulations, and can only check if it's equal to or not equal to, or searches, as well as numerical checks on number values.

Perhaps with a raw SQL query it could be done, but that would be an expensive check on an already expensive query. Simpler queries have brought down entire servers under moderate load, and I would expect any page containing your meta query will have major performance problems even without this addition.

My recommendation:

  • Figure out if the quote is too long when it's saved, and add a tag, not when you're querying
  • Look for that tag when querying, don't look at the character count
  • Don't use random order, instead try a random date
  • If the quote is always different then the page can't be cached, consider loading multiple quotes in javascript and doing the selection client side instead, or via a call to the REST API
  • Thank you for your answer, it helped me to figure an other approach. Instead of querying all posts by the string length from the custom field, I rather added a radio field next to the quote's textarea to add it or not to the home page. Then, I get all posts from the post type that are set to true. But I kept for the moment the random order and will find a solution later. – J-P-Dude Sep 28 '16 at 20:10
  • the random order will be a big performance boost, but congrats you sussed out another performance issue on your own by searching for what you want on the homepage, rather than excluding what you don't want. 1 is a simple filter, the other involves a search of every post in the database to check if it doesn't match – Tom J Nowell Sep 29 '16 at 2:05

I posted this answer on the original StackOverflow question, but:

It's great that you've found a likely more efficient solution, and I hate to disagree with a mod (sorry @TomJNowell!), but for reference: you totally can do this with WP_Query - you just need to use the meta_compare value of REGEXP and an appropriate meta_value as a regex to find the string lengths you're looking for:

$args = array(
    'post_type'         => 'experts',
    'posts_per_page'    => -1,
    'post_status'       => 'any',
    'meta_key'          => 'quote',
    'meta_value'        => '^.{1,75}$',
    'meta_compare'      => 'REGEXP',

$customQuery = new WP_Query($args);

This will find all posts of type experts (take note of posts_per_page and post_status) where the length of their meta_key value is between 1 & 75 characters.

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.