• Resolved Kim

    (@kimberry)


    Hi,
    Firstly, thanks for the great plugin!

    Not sure if it’s user error on my end or if anyone else has come across this.

    I’m using a query_id in my shortcode and it works great, however it seems to break the Search by Display Name. I have both snippets as per the FAQ in my functions.php.

    Do I need to make a change to the search query to match the query_id?

    Thanks for any help available.

    https://www.ads-software.com/plugins/simple-user-listing/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author HelgaTheViking

    (@helgatheviking)

    Please post your code.

    Thread Starter Kim

    (@kimberry)

    This is the code I’m using in the functions.php file.

    /* User List // Only get users who are approved */
    
    add_filter( 'sul_user_query_args', 'sul_custom_meta_query', 10, 2 );
    
    function sul_custom_meta_query( $args, $query_id ){
        // checking the query ID allows us to only target a specific shortcode
        if( $query_id == 'user_approved' ){
                $args['meta_query'] = array(
                                        'relation' => 'OR',
                                        array(
                                            'key'       => 'user_approved',
                                            'value'     => 'user_approved',
                                            'compare'   => '=',
                                            'type'      => 'CHAR',
                                        )
                                    );
    
        }
        return $args;
    }
    
    // Switch user search from user_login to display_name via query_where
    function kia_search_users_by_display_name( $query ) {
    
      if ( isset( $query->query_vars['query_id'] ) && $query->query_vars['query_id'] == 'simple_user_listing' ) {
         $query->query_where = str_replace( "user_login", "display_name", $query->query_where );
      }
    }
    add_action( 'pre_user_query', 'kia_search_users_by_display_name' );

    The shortcode that I’m using is [userlist query_id=”user_approved” role=”subscriber” meta_key=”last_name” orderby=”meta_value” order=”ASC”]

    Thanks again!

    Plugin Author HelgaTheViking

    (@helgatheviking)

    You need to change the query ID simple_user_listingto match your custom query id ofuser_approved`.

    // Switch user search from user_login to display_name via query_where
    function kia_search_users_by_display_name( $query ) {
    
      if ( isset( $query->query_vars['query_id'] ) && $query->query_vars['query_id'] == 'user_approved' ) {
         $query->query_where = str_replace( "user_login", "display_name", $query->query_where );
      }
    }
    add_action( 'pre_user_query', 'kia_search_users_by_display_name' );

    I can’t promise that both snippets will work together. I don’t see why not, but I’ve never tested them.

    Thread Starter Kim

    (@kimberry)

    Perfect! Well, at least it seems to be so far.
    Thanks for your help and quick response – really appreciate it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search by Display Name not working with query_id=’ is closed to new replies.