Support

Account

Home Forums General Issues Search Custom Fields by Wildcard

Helping

Search Custom Fields by Wildcard

  • I am trying to query posts by a custom field (not a subfield) using a wildcard. What I am trying to do is get all posts where the last name starts with the letter “A%” or “B%” etc etc. Cannot find any documentation on this, but surely it must be possible? MY WP Query code is below (but brings back 0 results).

    $group1 = new WP_Query(array(
    'post_type' => 'oral_histories',
    'posts_per_page' => -1,
    'meta_key'	=> 'last-name',
    'orderby'	=> 'meta_value',
    'order'		=> 'ASC',
    'meta_query'	=> array(
     'relation'		=> 'OR',
     array(
    'key'		=> 'last-name',
     'value'		=> 'A%',
    'compare'	=> 'LIKE'
    ),
     array(
    'key'		=> 'last-name',
    'value'		=> 'B%',
    'compare'	=> 'LIKE'
    )
    )
    ));
  • 
    $group1 = new WP_Query(array(
    'post_type' => 'oral_histories',
    'posts_per_page' => -1,
    'meta_key'	=> 'last-name',
    'orderby'	=> 'meta_value',
    'order'		=> 'ASC',
    'meta_query'	=> array(
     'relation'		=> 'OR',
     array(
    'key'		=> 'last-name',
     'value'		=> '^A',
    'compare'	=> 'REGEXP'
    ),
     array(
    'key'		=> 'last-name',
    'value'		=> '^B',
    'compare'	=> 'REGEXP'
    )
    )
    ));
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.