0

My question is for getting informations or directions about how to move for a task I have to do. This is just a request for advices due to the fact I'm not an expert in wordpress development.

My task would be to create a module (widget or just a query) which suggests registered users based on categories they've subscribed (user taxonomy like profession), just after somebody else post something.

I'll do an example:

  1. I post a job (form) selecting which profession it'd be dedicated for (user taxonomy);
  2. After the confirmation I get a widget showing 3 suggested users, matching the profession (user taxonomy) I've selected while posting.

I've created the user taxonomy but I don't really know where to start for this task. Do you know any article or question talking about something like it? Can you drive me to what I should study for it?

Thanks.

  • How did you add user taxonomy? Problem with user taxonomies is that there's no native support and you can't use it in user query! I suggest to use user meta instead and then you could use WP_User_Query(). Read it (there are plenty of examples on that page), try it and get back to us if it didn't work out. :-) – N00b May 4 '16 at 20:35
  • I've followed some tutorial for creating it like this one. So do you think I should work with user meta instead? Thanks @TwerkingN00b – Kleeia May 4 '16 at 20:45
  • And working with user meta how could I manage it from my dashboard? @TwerkingN00b – Kleeia May 4 '16 at 20:47
  • Do you have access to your database? I need to know how this taxonomy is linked to user. Does users table has a new column? Is there a new table called wp_user_relationships for example or is this relationship saved as user meta in wp_usermeta table.. Please add a taxonomy to one of your users and check it out. Then I can help you further. – N00b May 4 '16 at 20:52
  • I have access to my database and I see that these categories are added to wp_terms table. More specifically I've followed exactly this tutorial. @TwerkingN00b – Kleeia May 4 '16 at 21:06
1

You cannot query users by taxonomy. You will need to rethink your system. What would I do?


Example:

This code would go to the page template where your jobs are posted.

// This will check if form was submitted, it will not trigger/show on "normal" page load
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

    // This will get the profession from input after form is submitted & page reloaded
    // Change "profession" to the name of that input where user can choose profession
    $submitted_profession = $_POST['profession'];

    // Arguments for user query, see more from the user query link above
    $args = array(

        // How many to return?
        'number' => 3,   

        // User meta
        'meta_query' => array(
            array(
                'key'     => 'profession',  // Name of user meta key in database
                'value'   => $submitted_profession, // We get this from form, see above
                'compare' => '=' 
            )
        )
    );

    // Query itself
    $suggested_users_query = new WP_User_Query( $args );

    // Get the results
    $users = $suggested_users_query->get_results();

    // Check if there actually are users based on that criteria 
    if ( ! empty( $users ) ) {

        // All the output how you want your users to look like goes here 

        // I wrote a small example

        echo '<ul>';

        // Loop trough each user 
        foreach ( $users as $user ) {

            // Get all the user data
            // To find all the fields you could use, search "get_userdata()" online
            $user_info = get_userdata( $user->ID );
            echo '<li>' . $user_info->first_name.' ' . $user_info->last_name . '</li>';
        }

        echo '</ul>';

    } 
    // No users with that profession
    else {

        echo 'No users with profession ' . $submitted_profession;
    }
}
  • Wooow! Thank you for your deep and useful support! Great! – Kleeia May 4 '16 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.