WordPress.org

Support

Support » Plugins and Hacks » [Resolved] New Filter

[Resolved] New Filter

Viewing 15 replies - 1 through 15 (of 23 total)
  • I forgot this line of code after the new filter code:

    if ( empty ($users) ) {
    $users = $sul_users->get_results();
    }

    if this is not present there would be no results without a search.

    Plugin Author helgatheviking

    @helgatheviking

    So what does your new block of code look like, combined? Does this work?

    $users = apply_filters( 'sul_users_results', $sul_users->get_results(), $list_id );

    instead of:

    $users = $sul_users->get_results();

    I’m happy to add a filter. Afterwards, hopefully you will share how you achieved this!

    In simple-user-listing.php I added:

    $sul_users = new WP_User_Query( $args );
    // The authors object.
    $users = $sul_users->get_results();
    
    //new filter
    $users = apply_filters( 'sul_users_results', $users, $list_id );
    
    //conditional
    if ( empty ($users) ) {
    $users = $sul_users->get_results();
    }
    
    do_action( 'simple_user_listing_before_loop', $list_id );

    and in function.php I added this functions

    function kia_meta_search( $args ){
    
      // this $_GET is the name field of the custom input in search-author.php
        $search = ( isset($_GET['as']) ) ? sanitize_text_field($_GET['as']) : false ;
    
        if ( $search ){
                   /* There's a reason we're adding the action from here, you'll see later why. */
        add_action( 'pre_user_query', 'maor_twitter_help_main' );
    
        /* Create a new WP_User_Query object, limit recordset to 10 */
        $wp_user_search = new WP_User_Query(
            array(
             'number' => 12,
    	'role' => 'Author' //I just wanted to check the Authors
            )
        );
    
        /* Make sure to remove this action since it may affect other user queries around the site. */
        remove_action( 'pre_user_query', 'maor_twitter_help_main' );
    
        /* Get them users! */
        $users = $wp_user_search->get_results();
        return $users;
    
        }
    
    }
    
    add_filter('sul_users_results', 'kia_meta_search');
    
    function maor_twitter_help_main( $query ) {
        global $wpdb;
      $search = ( isset($_GET['as']) ) ? sanitize_text_field($_GET['as']) : false ;
    
        if ( $search ){
        /* The display_name value you're looking to match */
        $display_name = $search;
    
        /* Search can either be for an exact match or a partial match */
        if ( $use_like_syntax = true ) {
            $query->query_where .= $wpdb->prepare( " AND $wpdb->users.display_name LIKE %s", '%' . like_escape( $display_name ) . '%' );
        } else {
            $query->query_where .= $wpdb->prepare( " AND $wpdb->users.display_name = %s", $display_name );
        }
    }
    }
    Plugin Author helgatheviking

    @helgatheviking

    I looked at this just now when I should be sleeping. I don’t think you need a new hook, but you can take advantage of the fact that I’ve already give the query a query_var called “query_id”. You can use this to target your pre_user_query filter function to only work on this particular query.

    add_action( 'pre_user_query', 'maor_twitter_help_main' );
    
    function maor_twitter_help_main( &$query ) {
    
    	if ( isset( $query->query_vars['query_id']) && 'simple_user_listing' ==  $query->query_vars['query_id'] ) {
    
    		global $wpdb;
    
    		/* The display_name value you're looking to match */
    		$display_name = ( isset($_GET['as']) ) ? sanitize_text_field($_GET['as']) : false ;
    
    		if ( $display_name ){
    
    			/* Search can either be for an exact match or a partial match */
    			if ( $use_like_syntax = true ) {
    				$query->query_where .= $wpdb->prepare( " AND $wpdb->users.display_name LIKE %s", '%' . like_escape( $display_name ) . '%' );
    			} else {
    				$query->query_where .= $wpdb->prepare( " AND $wpdb->users.display_name = %s", $display_name );
    			}
    
    		}
    	}
    }

    To make the where statement come out correctly, I had to filter the query args and kill the search argument, since by default WP searches by username. Leaving it in wasn’t working for me. Let me know how that goes.

    It works, thank you very much.

    Now I came across another problem, the number of users that $sul_users->get_total() gives varies from page 1 to page 2 which is breaking my pagination.

    I’ll try to find a cause but any help is appreciated.

    Thanks in advance.

    Plugin Author helgatheviking

    @helgatheviking

    I have no idea. You shouldn’t need the $sul_users->get_total() code any more though since we’re modifying the user query via pre_user_query.

    Awesome, I’ll wait for the next update to fix this then.

    Thanks for all the help, I’ve only seen now that your solution doesn’t specify the search for only the author role and unfortunately I need that.

    Plugin Author helgatheviking

    @helgatheviking

    I thought we resolved that I don’t need to add any new hooks/filters…. as what you were trying to do can be achieved without it. I don’t have any updated version planned.

    I edited my last post, sorry.

    Since you don’t have any updated version planned I temporarily fixed the issue (with the pagination) by changing the $total_pages to how it was in the original tutorial.

    Thanks again for all the wonderful support.

    Plugin Author helgatheviking

    @helgatheviking

    You’re welcome. If you dig the plugin, would you kindly leave a review?
    http://wordpress.org/support/view/plugin-reviews/simple-user-listing

    I’ll, did you read what I wrote in my previous post?

    “I’ve only seen now that your solution doesn’t specify the search for only the author role and unfortunately I need that.”

    Plugin Author helgatheviking

    @helgatheviking

    About $total_pages? I don’t know the answer to that. If you can show me that it is definitely a problem with my plugin and not with your implementation then I will try to look at it when I’m free.

    If you only want to show authors you can specify that in the shortcode as shown in the Directions

    [userlist role="author" number="5"]

    Or you could filter it into the query args via sul_user_query_args. I wasn’t offering a full solution so much as proving that I didn’t need to add a new hook.

    I’m talking about the function to search by $display_name, your function searches every role and I need to constrain it to the author role only.

    About the pagination error, I’ll check in a clean installation if it still happens.

    It still happens in a clean wordpress install, if you want to see what I’m talking about do the following:

    -Install and activate plugin;
    -Create page and add shortcode with role=”author” number=”2″;
    -Create 8 user with the role author;
    -Go to your page that has the shortcode and try to see all 8 users through pagination.

    8 users equal to 4 pages of 2 users per page but in the page before the last the plugin seems to change something that makes the total_users equal to the number of users already shown, making the last page inaccessible.

    I tried to find a cause but couldn’t and can’t spend more time trying to solve this.

    Plugin Author helgatheviking

    @helgatheviking

    I’m pushing out an update that should maintain the “role” parameter on search. Then if you’ve set the role to “author” in the shortcode, then the search should only return authors even with my code implemented.

    It looks like there is a problem on the query_where string if there is a search parameter in the query vars. I’ve found that you must get rid of the default search in order to implement display name searching. You can do this by filtering the query args.

    // Switch the WP_User_Query args to a kill normal search
    function kia_display_name_args( $args ){
    
      // this $_GET is the name field of the custom input in search-author.php
      $display_name = ( isset($_GET['as']) ) ? sanitize_text_field($_GET['as']) : false ;
    
    	if ( $display_name ){
            $args['search'] = '';
        }
    
        return $args;
    }
    add_filter('sul_user_query_args', 'kia_display_name_args');

    Then the pre_user_query filter from before should work. I hope it does because it is a neat idea, but if it doesn’t I can’t put any more time into this custom implementation.

    ——–
    I also can’t reproduce your pagination error. I have 8 editors and with number="2" I can paginate to the 4th page with no problem as the next link appears on page 3 with no problems.

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Resolved] New Filter’ is closed to new replies.