Skip to content
Custom fields for WordPress that are quick to build and quick to load. Get Pro

Stop writing meta_query: find posts by their fields in five lines

Easy recipes, under 30 minutes, Italian or French, quickest first. In WordPress that is 26 lines of nested arrays. With ugly_find() it is five, and you can read them out loud.

By the UglyDev team17 November 20266 min read

26 lines → 5 The same query, written as a sentence

Illustration for: Stop writing meta_query: find posts by their fields in five lines

In short

  • A filtered query that takes 26 lines of meta_query takes five lines with ugly_find().
  • You write comparisons as plain strings ('< 30', 'between 5 and 10'), so there is no NUMERIC type to forget.
  • The result is a normal WP_Query, so your loops, pagination and plugins keep working.
  • ugly_find() is in the free plugin.

Every WordPress developer has written the same wall of arrays: relation, key, value, compare, type. It works. It is also the part of the code nobody wants to touch twice. This is a WordPress meta_query alternative that reads like the sentence you had in mind.

The question

You have a content type, Recipe, with a few custom fields. You want the easy ones, under 30 minutes, Italian or French, the quickest first. That is one sentence. This is what it costs in plain WordPress:

PHP
$q = new WP_Query( array(
	'post_type'      => 'recipe',
	'posts_per_page' => 12,
	'meta_key'       => 'minutes',
	'orderby'        => 'meta_value_num',
	'order'          => 'ASC',
	'meta_query'     => array(
		'relation' => 'AND',
		array(
			'key'     => 'difficulty',
			'value'   => 'easy',
			'compare' => '=',
		),
		array(
			'key'     => 'minutes',
			'value'   => 30,
			'type'    => 'NUMERIC',
			'compare' => '<',
		),
		array(
			'key'     => 'cuisine',
			'value'   => array( 'italian', 'french' ),
			'compare' => 'IN',
		),
	),
) );

26 lines. Three places to make a typo, and a comparison type (NUMERIC) that you have to remember, or the result is quietly wrong: 9 sorts after 30 when WordPress thinks they are text.

The same question, in UglyFields

PHP
$q = ugly_find( 'recipe', array(
	'difficulty' => 'easy',
	'minutes'    => '< 30',
	'cuisine'    => array( 'italian', 'french' ),
), array( 'order' => 'minutes asc', 'limit' => 12 ) );

Five lines, and they read like the sentence. What you get back is a normal WP_Query, so your loop, your pagination and every plugin that expects a query keep working. There is nothing new to learn on the output side.

The field builder of UglyFields with a group of fields
The fields you search by are the ones you named in the builder.

What you can ask

ugly_find() takes the value the way you would say it. The comparison is written inside the string, so there is no separate key for it.

WriteIt means
'easy', 12Is equal to
'< 30', '>= 5', '!= x'Compared
'between 5 and 10'A range
'~oat'Contains the text
'empty', 'filled'Has nothing, or has something
array( 'a', 'b' )Any of them
true, falseA switch is on, or off

A relationship field can be asked too. “The posts that point at this one” is a value, not a join you build by hand.

Three searches you will actually write

A price range, cheapest first

PHP
$rooms = ugly_find( 'room', array(
	'price' => 'between 80 and 140',
), array( 'order' => 'price asc' ) );

Everything that has a PDF

PHP
$guides = ugly_find( 'guide', array( 'download' => 'filled' ) );

Posts on sale and in stock

PHP
$items = ugly_find( 'product', array(
	'on_sale'  => true,
	'in_stock' => true,
) );

The third one is the case that shows the difference best. With a switch field you write true, and the plugin knows how a switch is stored. You do not have to remember that WordPress saved it as 1.

Why it is less error-prone

  • You write '< 30'. There is no comparison type to remember, and no NUMERIC to forget.
  • The fields are the ones you named in the builder, so the query uses the words your team already uses.
  • The query is the part you will read again in six months. It should look like what it does.
  • It is a query, not a new data layer: if you need something exotic, you can still hand a meta_query to the same function.

When you still want plain WP_Query

Be fair to the old way: if you write one query on one site and it never changes, meta_query is fine and has no dependency. ugly_find() earns its place when you have catalogs, directories, filters or any list that depends on what editors typed, and you have more than a couple of queries to keep alive.

It is in the free plugin. Try it on a real site, or read the overview of relations and queries if your lists point at other posts.

Sorting, limits and pages

The second array of ugly_find() takes the things that are not about the fields: the order, the number of results and the page. Because the result is a normal WP_Query, you do not lose pagination.

PHP
$page  = max( 1, (int) get_query_var( 'paged' ) );
$rooms = ugly_find( 'room', array(
	'price' => 'between 80 and 140',
	'beds'  => '>= 2',
), array(
	'order' => 'price asc',
	'limit' => 9,
	'page'  => $page,
) );

while ( $rooms->have_posts() ) : $rooms->the_post();
	get_template_part( 'parts/room' );
endwhile;
wp_reset_postdata();

Reading it once is enough: rooms between 80 and 140, at least two beds, cheapest first, nine per page. That is the whole query.

Filters that come from the URL

A directory page often filters by what the visitor chose. The values arrive as strings, so validate before you ask:

PHP
$cuisine = isset( $_GET['cuisine'] ) ? sanitize_key( $_GET['cuisine'] ) : '';
$where   = array( 'difficulty' => 'easy' );

if ( $cuisine ) {
	$where['cuisine'] = $cuisine;
}

$q = ugly_find( 'recipe', $where );

Building the conditions as a plain array keeps each filter on one line, and adding a new filter later is one more if, not a rewrite of nested structures.

Is it slower?

The query runs through WordPress in the usual way, so the cost is the same kind of cost you would pay with a hand-written meta_query: it depends on your data and your indexes. What changes is how much of it you have to think about while you write. For large data sets, look at the same things you always would: the number of meta conditions, the order, and whether you need all the results at once.

Real examples you can adapt

A directory of businesses

A directory page often needs to filter by area, by whether the business is open and by a rating. In plain WordPress that is three nested conditions and a type for the rating. Written as a question:

PHP
$places = ugly_find( 'business', array(
	'area'   => array( 'north', 'center' ),
	'open'   => true,
	'rating' => '>= 4',
), array( 'order' => 'rating desc', 'limit' => 20 ) );

Upcoming events

For events you usually want everything from today, soonest first. The date is a field; the question is about the field.

PHP
$events = ugly_find( 'event', array(
	'starts' => '>= ' . gmdate( 'Y-m-d' ),
), array( 'order' => 'starts asc', 'limit' => 10 ) );

Search that includes the fields

If visitors search a catalog, you can pass the search text in the options and keep the field conditions as they are. The results stay one normal query, so everything that works on a query keeps working.

Checklist before you ship a query

  1. Name it. Wrap the query in a small function with a name that says what it finds, such as get_easy_recipes().
  2. Limit it. Pass a limit, and paginate for long lists.
  3. Check the empty case. Decide what the page says when there are no results.
  4. Test with real data. A query that is fast with ten posts can be slow with ten thousand.
  5. Reset after the loop. Call wp_reset_postdata() so the rest of the page is not affected.

These habits help with any query, and a readable query makes each of them easier. To see how the fields you search are chosen in the first place, read every field type in one minute.

Questions people ask

What is the best WordPress meta_query alternative?

If you use custom fields heavily, a function that takes the fields as they are, such as ugly_find(), removes the nested arrays. It returns a normal WP_Query, so nothing else in your theme changes.

Does ugly_find() return a WP_Query?

Yes. You can use your usual loop, pagination and plugins with the result.

Can I compare numbers and ranges?

Yes. You write the comparison in the value: '< 30', '>= 5' or 'between 5 and 10'. There is no comparison type to remember.

Does it work with relationship fields?

Yes. Asking for the posts that point at another post is a value in the same array, not a join you build by hand.

Is ugly_find() free?

Yes, it is part of the free plugin.

Can I paginate the results of ugly_find()?

Yes. You pass the page in the second array, and the result is a normal WP_Query, so pagination works as usual.

Can I filter by values from the URL?

Yes. Validate the value, add it to the array of conditions and pass the array to ugly_find().

How do I find upcoming events with custom fields?

Ask for the events whose date field is greater than or equal to today, and order by the date. With ugly_find() it is one condition and one order.

Should I wrap queries in a function?

Yes. A named function such as get_easy_recipes() keeps the query in one place and makes templates easier to read.

Ready to try it?

Download the free plugin, build your first group in a few minutes and keep it in your theme. No time limit, no sign-up.

Download it freeSee Pro

Next article What we learned about moving a site away from ACF

Free and Pro

Start free. Upgrade when you grow.

No sign-up, no time limit, and nothing locks you in.
Put it on a site and see how it feels.

The UglyFields mark: a U made of blocks, with the orange one about to fit