Support

Account

Home Forums ACF PRO WP_User_Query issue

Solved

WP_User_Query issue

  • Hi guys!
    I have an issue, which i can’t solve, so i decided to write you here.
    http://findacpatoday.flywheelsites.com/find-a-tax-professional/ – here is a filterable subscribers directory, there is a filter on top of the page, which is filtering by 3 fields:
    1. City – text field on backend
    2. State – select field on backend
    3. Category – Multi select field on backend
    The issue is when users use filter, some results shows dubbed and in some case shows subscribers which doesn’t have this value. Here you can see example: http://findacpatoday.flywheelsites.com/find-a-tax-professional/?city=Ogden

    The filter works on _GET variables.
    here is a code of a form:

    <form id="main-form" action="<?php echo get_the_permalink(131); ?>"  method="get">
      <input id="city" name="city" type="text" placeholder="City" value="">
      <?php
    
      $field_key = "field_5aac1abbd04cb";
      $field = get_field_object($field_key);
    
      if( $field )
      {
        echo '<select id="location" name="location">';
          echo '<option></option>';
          foreach( $field['choices'] as $k => $v )
          {
            echo '<option value="' . $k . '">' . $v . '</option>';
          }
        echo '</select>';
      }
    
      $field_key = "field_5ab00549ff1dd";
      $field = get_field_object($field_key);
      if( $field )
      {
        echo '<select id="category" name="category">';
        echo '<option></option>';
          foreach( $field['choices'] as $k => $v )
          {
            echo '<option value="' . $k . '">' . $v . '</option>';
          }
        echo '</select>';
      }
      ?>
      <button id="form-submit" type="submit"><?php _e('Search', 'foundation'); ?></button>
    </form>

    And here is a query:

    <?php
    $num = 10;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if($paged == 1){
        $offset = 0;  
    } else {
        $offset = ( $paged - 1 ) * $num;
    }
    //WP_User_Query arguments
    $args = array (
        'role'		 => 'subscriber',
        'order'      => 'ASC',
        'orderby'    => 'display_name',
        'number'	 => $num,
        'offset' 	 => $offset,
        'meta_query' => array(
            'relation' => 'AND',
        )
    );
    if($city != ''){
        $args['meta_query'][] = array(
            array(
    	    'meta_key'     => 'city',
    	    'value'   => $city,
    	    'compare' => 'LIKE',
    	 ),
        );
    }
    if($loc != ''){
        $args['meta_query'][] = array(
            array(
    	    'meta_key'     => 'state',
                'value'   => $loc,
                'compare' => 'IN',
    	),
        );
    }
    if($cat != ''){
        $args['meta_query'][] = array(
            array(
    	    'meta_key'     => 'specialties',
    	    'value'   => '"'.$cat.'"',
                'compare' => 'LIKE'
    	)
        );
    }
    // Create the WP_User_Query object
    $wp_user_query = new WP_User_Query($args);
    // Get the results
    $authors = $wp_user_query->get_results();

    Thank you for your attention!

  • Already fixed. It was because of nested field in meta key, for example, if you have cite field inside left_column group field, you shoud set key as left_column_city.
    And the second issue i used meta_key instead of key.
    So the right arguments should be like this:

    $args = array (
        'role'		 => 'subscriber',
        'order'      => 'ASC',
        'orderby'    => 'display_name',
        'number'	 => $num,
        'offset' 	 => $offset,
        'meta_query' => array(
            'relation' => 'AND',
        )
    );
    if($city != ''){
        $args['meta_query'][] = array(
            array(
    	    'key'     => 'left_column_city',
    	    'value'   => $city,
    	    'compare' => 'LIKE',
    	 ),
        );
    }
    if($loc != ''){
        $args['meta_query'][] = array(
            array(
    	    'key'     => 'left_column_state',
                'value'   => $loc,
                'compare' => '=',
    	),
        );
    }
    if($cat != ''){
        $args['meta_query'][] = array(
            array(
    	    'key'     => 'left_column_specialties',
    	    'value'   => $cat,
                'compare' => 'LIKE'
    	)
        );
    }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘WP_User_Query issue’ is closed to new replies.