• I have an ajax function that runs the following WP_User_Query:

            $args2 = array(
                'meta_query' => array(
                        'gender' => array(
                            'key'     => 'gender',
                            'value'   => 'female',
                             'compare' => '='
                        )
                        ),
                        'number' => 10
             );
            $user_query = new WP_User_Query( $args2 );

    The query is returning unfiltered results, basically ignoring the meta_query. It works fine if I run it from functions.php or from a template file, but not from ajax. Any idea why?

    This is the?request?string when it works:

                 SELECT SQL_CALC_FOUND_ROWS wp_users.ID
                    FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id 
                    )
                    WHERE 1=1 AND ( 
                    ( wp_usermeta.meta_key = 'gender' AND wp_usermeta.meta_value = 'female' )
                    )
                    ORDER BY user_login ASC
                    LIMIT 0, 10

    And this is the one from the ajax function:

                    SELECT SQL_CALC_FOUND_ROWS wp_users.ID
                    FROM wp_users
                    WHERE 1=1
                    ORDER BY user_login ASC
                    LIMIT 0, 10
Viewing 7 replies - 1 through 7 (of 7 total)
  • There could be a few reasons why the WP_User_Query is ignoring the meta_query when run from an AJAX function. Here are a few things you can try to troubleshoot the issue:

    1. Make sure that you are correctly enqueuing the script that contains the AJAX function and that the script is correctly registered and localized.
    2. Check the $_POST data that is being passed to the AJAX function to make sure that it contains the correct meta_query arguments.
    3. Make sure that the meta_query arguments are being correctly passed to the WP_User_Query constructor. You can try printing out the $args2 array before the WP_User_Query is run to verify that the meta_query is present.
    4. Check the WordPress debug log for any errors or warnings that may be related to the WP_User_Query or the AJAX function.
    5. Make sure that the usermeta table and the gender meta key exist in the database and that they have the correct permissions.

    Hope this helps.

    Thread Starter cguidog

    (@cguidog)

    That answer is from ChatGPT, and it doesn’t solve the issue.

    Moderator bcworkz

    (@bcworkz)

    Is your user query really exactly as you had shown here? You do not use any data passed from the Ajax call? Your actual query only gets “female” values every time? The reason I ask is there appears to be something else wrong with your Ajax processing because when I use your exact query args in one of my Ajax calls, the resulting SQL is exactly as you get in your non-Ajax request, i.e. my Ajax’s SQL includes all the expected usermeta criteria. If the meta_query syntax is wrong, WP will ignore that particular arg. However, all appears to be correct as you’ve posted it.

    Another possibility is some other code is filtering user queries and mis-applying the filter adjustments to your Ajax process. Try removing all extraneous code by deactivating all plugins and switching to a default twenty* theme. You’ll need to temporarily incorporate all of your Ajax related code (and only the Ajax related code) into the chosen default theme.

    Thread Starter cguidog

    (@cguidog)

    @bcworkz These are placeholder values. I wanted to test this way before passing the actual parameters; in reality the query will have several parameters.

    I have two functions. This is the function that runs the query and it’s located in functions.php

    function export_users_function_test() {
        $args = array(
            'number' => 10,
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'employee',
                    'value' => '1',
                    'compare' => '='
                ),
                array(
                    'key' => 'gender',
                    'value' => 'female',
                    'compare' => '='
                ),
                array(
                    'key' => 'member',
                    'value' => '1',
                    'compare' => '='
                )
            )
        );
         $user_query = new WP_User_Query( $args );
        return $user_query;
    }

    This is the function located in my ajax file, which calls the function in functions.php:

        public function export_users_function() {
            $params = $_POST['params'] ?: null;
             $query = export_users_function_test() ?: null;
            $users = $query ? $query->results : null;
          
            $header = '"Email",';
    
            while(have_rows('file_columns', 'option') ): the_row();
              $header .= '"' . get_sub_field('column_name') . '",';
            endwhile;
            $header .=  "\r\n";
            if ($users) {
                foreach ($users as $user) {
                    $header .= '"' . $user->user_email . '",';
                    while(have_rows('file_columns', 'option') ): the_row();
                    $header .= '"' . get_user_meta( $user->ID, get_sub_field('user_meta_key'), true) . '",';
                  endwhile;
                  $header .=  "\r\n";
                }
            }
            echo wp_send_json($header);
          }
    • This reply was modified 1 year, 10 months ago by cguidog.
    • This reply was modified 1 year, 10 months ago by cguidog.
    • This reply was modified 1 year, 10 months ago by cguidog.

    Did you really need to call me out though? trying to build a reputation here on the forum.. thanks bud

    Moderator bcworkz

    (@bcworkz)

    Sometimes people “clean up” examples that they post here, but the problem they face gets removed as part of their clean up effort. Doesn’t appear to be the case here, but I wanted to be sure. I didn’t test your full code, but I did test your query as part of my own Ajax callback. Of course without the needed meta data nothing was found, but the SQL that WP generated is

    				SELECT SQL_CALC_FOUND_ROWS wp_users.ID
    				FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id )  INNER JOIN wp_usermeta AS mt1 ON ( wp_users.ID = mt1.user_id )  INNER JOIN wp_usermeta AS mt2 ON ( wp_users.ID = mt2.user_id )
    				WHERE 1=1 AND ( 
      ( wp_usermeta.meta_key = 'employee' AND wp_usermeta.meta_value = '1' ) 
      AND 
      ( mt1.meta_key = 'gender' AND mt1.meta_value = 'female' ) 
      AND 
      ( mt2.meta_key = 'member' AND mt2.meta_value = '1' )
    )
    				ORDER BY user_login ASC
    				LIMIT 0, 10

    Looks like it should do the job, it’s certainly not the WHERE 1=1 clause you are getting. Also, I know of no reason in core WP why an Ajax query would be any different than a normal, non-Ajax query. The only explanation I can imagine is some other code is inappropriately altering your query via some action or filter hook. Review the last paragraph of my previous reply.

    Thread Starter cguidog

    (@cguidog)

    @bcworkz I couldn’t get this to work even after disabling all plugins and using a default theme. I ended up finding an alternative non-ajax solution to my problem.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WordPress WP_User_Query ignores the `meta_query` when called from an Ajax functi’ is closed to new replies.