WP Meta Query with Custom Field Post Object

Using Advanced Custom Fields with WordPress is an absolute pleasure, and the fact that you can reference other post types as options within fields is an awesome feature.

The only issue comes when you try to execute a WP_Query meta query on the data set on your post types. WordPress stores data in wp_postmeta as expected, but it only stores the post ID. This is fine, but if you want to search by the post value (which is automatically the post title), rather than ID then you’re in trouble.

There is a way around this though – by using the WordPress method get_page_by_title() you can convert your title into the post object, grab the ID, then complete your meta query.

Here’s an example:

$post = get_page_by_title( 'My Post Title', OBJECT, 'post_type_here');
$args = array(
		'post_status' => 'publish',
		'meta_key' => 'post_object_type',
		'meta_value' => $post->ID
$query = new WP_Query($args);

if ( $query->have_posts() )
	while ( $query->have_posts() )
		// add code here

This isn’t 100% fool-proof though because if you’ve got duplicate page titles it will only return the lowest-matching ID. If your titles are unique, you’ll be laughing.