I'm using the Meta Box plugin to create custom post types and fields. There is a record type teachers, and I'm trying to display the lessons associated with him on the page of a certain teacher. The code looks like this:

$queryArgs = array(
            'post_type' => 'lessons',
            'order' => 'ASC',
            'posts_per_page' => -1,
            'orderby' => 'meta_value',
            'meta_key'  => 'lessons_teacher',
            'compare' => 'LIKE',
            'meta_query' => [
                    'key'  => 'post_name',
                    'value'   => 'john-smith',

and here it is not entirely clear how to compare lessons by the value of [post_name], because the field is an array, i.e. a set of keys - values... and [post_name] is the key to which, in fact, you need to refer..


1 Answer 1


I'm not familiar with the Meta Box plugin or it's capabilities (and please note that third-party products are considered off-topic here!) - but regarding the question in the scope of core WordPress alone...

For clarity's sake, I envision the lessons_teacher meta-value to look similar to

[['post_name' => 'john_smith'], ['post_name' => 'jane_doe']]

SQL - and by extension WordPress's meta-query functionality - is not designed to be used against complex serialized data; SQL has no concept of PHP serialization - it simply treats complex serialized data as a string. While it is possible to work around this limitation to an extent, it should be noted that such a work-around comes with performance implications and a degree of fallibility - the mechanism is not robust and may yield the wrong result depending on the content of the serialized value.

The best solution is to store the associated teachers as singular post names in multiple meta data fields with the same name, or otherwise break the complex serialized data down into multiple non-serialized fields.

That in mind, the work-around consists of querying against the value which is actually stored in the database rather than it's unserialized PHP representation. The array of associative arrays above would be stored as the string:


In effect, you would want to query for posts which have a meta value for the key lessons_teacher containing the sequence s:9:"post_name";s:<post_name length>:"<post_name>";

$teacher_post_name = 'john-smith';
$teacher_meta_val  = sprintf(
$query_args        = [
  'post_type'      => 'lessons',
  'posts_per_page' => -1
  'meta_key'       => 'lessons_teacher',
  'meta_value'     => $teacher_meta_val,
  'meta_compare'   => 'LIKE',


  • You could also use a REGEXP comparison operator here - but either one has serious performance implications for the query.
  • I've omitted your orderby meta-value argument because it most certainly will not achieve what you'd like - it would order results by the serialized string.
  • You could simplify the meta_value to just $teacher_post_name - but that might risk additional false-positives if a teacher post name (or something with the same characters) appears elsewhere in the complex serialized data.

Ultimately, I would not ship something containing this code - the additional overhead and risk of failure is too great when simpler and more efficient alternatives are available.

By contrast, if you were to store each associated teacher's post_name by itself in it's own lessons_teacher meta-field, the query would would be a simple

$query_args        = [
  'post_type'      => 'lessons',
  'posts_per_page' => -1
  'meta_key'       => 'lessons_teacher',
  'meta_value'     => $teacher_post_name,

with substantially greater performance and virtually no risk of false-positives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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