I am setting up an advanced search page with ACF fields with a custom post type in wordpress.

In my example I am sending 'Students' in my url parameters as ?licenseType=Students

Search Example

*EDIT adding PHP code:

<select id="licenseType" name="licenseType">
<option value="">All License Types</option>
            <?php $field = get_field_object('software_license_type');               
                    if( $field )
                        foreach( $field['choices'] as $key => $value )
                            echo '<option value="'.$key.'" '.((isset($_GET['licenseType']) && $_GET['licenseType'] == $value)?' selected':'').' >'.$value.'</option>';

I have registered my query_vars

 add_filter( 'query_vars', 'software_register_query_vars' );
 function software_register_query_vars( $vars ) {
    $vars[] = 'licenseType';
  return $vars;

and set up my pre_get_posts filter

function software_pre_get_posts( $query ) {
  if ( is_main_query() && $query->get('post_type') == 'uc_software' ) {
    $query->set( 'orderby', 'title' ); 
    $query->set( 'order', 'ASC' ); 

    $meta_query = array(); 

    if( !empty( get_query_var( 'licenseType' ) ) ){
    $meta_query[] = array( 'key' => 'software_license_type', 'value' => 
    get_query_var( 'licenseType' ), 'compare' => 'LIKE' );
// ...
//I will add more fields here...
// ...  
    if( count( $meta_query ) > 1 ){
      $meta_query['relation'] = 'AND';

    if( count( $meta_query ) > 0 ){
      $query->set( 'meta_query', $meta_query );

When I view the query that is being created I see

SELECT wp_125_posts.* FROM wp_125_posts INNER JOIN wp_125_postmeta ON ( 
wp_125_posts.ID = wp_125_postmeta.post_id ) WHERE 1=1 AND ( ( 
wp_125_postmeta.meta_key = 'software_license_type' AND 
wp_125_postmeta.meta_value LIKE 
) ) AND wp_125_posts.post_type = 'uc_software' AND (wp_125_posts.post_status = 'publish' 
OR wp_125_posts.post_status = 'acf-disabled' OR wp_125_posts.post_status = 'private') 
GROUP BY wp_125_posts.ID ORDER BY wp_125_posts.post_title ASC

What is the random string that is being captured along with my query_var? Is there a way I can retrieve my query_var without it? I see the same thing when I use $_GET['licenseType'].

  • What’s the code for the form? Where the select field is made. – Jacob Peattie Nov 16 '17 at 15:44
  • I don't think that is relevant but I will add it. My url does say ...com?/licenseType=Students&.... – Bridget Arrington Nov 16 '17 at 16:55

This is a symptom of having a % character in your meta query value. Since 4.8.3 % characters are being escaped (actually replaced by a semi random string) and should be unescaped before executing the sql query.

You should check with the plugin author what would be the best way to handle it, but in your specific case I would say that slight redesign to your code would be a better solution than any unescaping code. LIKE is an expensive operation better be avoided and based on your UI you can just make a strict compare of values instead.

|improve this answer|||||
  • I am the plugin author :D I changed to = compare and it works! Thank you! – Bridget Arrington Nov 16 '17 at 17:01
  • lol meant ACF as it is a common problem with it and I thought they might have some special solution – Mark Kaplun Nov 16 '17 at 17:47
  • I remember why I used LIKE, now. When I add multiple meta_query arrays the search will work with LIKE but not = – Bridget Arrington Nov 16 '17 at 18:55
  • are you storing it in serialized form?. The additional danger with a Like is that it will match things that it should not. – Mark Kaplun Nov 16 '17 at 21:13
  • Yes, it IS stored in serialized form. Thanks again!! – Bridget Arrington Nov 17 '17 at 16:09

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.