WordPress.org

Ready to get started?Download WordPress

Forums

Simple User Listing
[resolved] New Filter (24 posts)

  1. DeadForce
    Member
    Posted 9 months ago #

    First of all let me say Thanks for an wonderful plugin.

    I was trying to implement search by display_name using this tutorial: http://maorchasen.com/blog/2012/09/19/using-wp_user_query-to-get-a-user-by-display_name/ and I succeeded but I needed to add this filter to the core .php file:

    $users = apply_filters( 'sul_users_results', $users, $list_id );

    after

    $users = $sul_users->get_results();

    If you can add it by default for future version I would be very grateful.

    Thanks again for the wonderful plugin.

    http://wordpress.org/extend/plugins/simple-user-listing/

  2. DeadForce
    Member
    Posted 9 months ago #

    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.

  3. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    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!

  4. DeadForce
    Member
    Posted 9 months ago #

    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 );
        }
    }
    }
  5. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    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.

  6. DeadForce
    Member
    Posted 9 months ago #

    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.

  7. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    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.

  8. DeadForce
    Member
    Posted 9 months ago #

    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.

  9. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    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.

  10. DeadForce
    Member
    Posted 9 months ago #

    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.

  11. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

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

  12. DeadForce
    Member
    Posted 9 months ago #

    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."

  13. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    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.

  14. DeadForce
    Member
    Posted 9 months ago #

    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.

  15. DeadForce
    Member
    Posted 9 months ago #

    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.

  16. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    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.

  17. DeadForce
    Member
    Posted 9 months ago #

    Awesome, I'll eagerly wait for the update.

    Does this function fixes something for me? My 'temporary' method is working fine for my needs.

    ---

    About the pagination error, I tried again with the users as editors in a fresh installation of wordpress with just your plugin installed and same problem occurred.

    Did you define the role in the shortcode? If you didn't it'll work fine.

  18. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    You said the search by display name was not being restricted to authors, and you were right. The update maintains the shortcode's defined "role" parameter on search.

    Yes the function I posted fixes something for you. The two functions, I've posted together implement search by display name, restricted by role (assuming you define role in your shortcode), and without modifying my plugin. Your modification to my plugin actually causes WP_User_Query to be run twice, which isn't necessary.

    RE: pagination, yes i defined the role in my shortcode.

    [userlist number="2" role="editor" ]
  19. DeadForce
    Member
    Posted 9 months ago #

    I understand now, wonderful support thanks!

    About the pagination I can't understand why it's happening since there's no logical reason for it to happen.

    May it be because I'm testing locally?

    If you wish I can send you images of the problem.

  20. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    MU or Single site? Have you removed your customizations from my plugin? You can screenshot it if you want, but it will still be hard for me to fix until I can reproduce it and right now I can't. I'm testing locally too.

  21. DeadForce
    Member
    Posted 9 months ago #

    Single site, every thing is installed without customizations, may it be a server side issue?

    Sorry for the late response.

  22. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    Running any other plugins? I'm really at a loss here.

  23. DeadForce
    Member
    Posted 8 months ago #

    I tried it with a clean version of wordpress and the problem still exists, I've implemented another plugin now that deals with pagination ( Infinite - Scrolling ) and that solves it without changing the core.

    Thanks for all the wonderful support.

  24. helgatheviking
    Member
    Plugin Author

    Posted 8 months ago #

    Sorry I couldn't solve this one for you. SUL does support WP Pagenavi now, so that might help.

Reply

You must log in to post.

About this Plugin

About this Topic