Support » Fixing WordPress » Filter by User Custom Field

  • So I have few extra fields for my users to fill out, and now I would like to build a page that filter by these fields and am having issues. My fields are:

    update_usermeta( $user_id, 'location', $_POST['location'] );
    update_usermeta( $user_id, 'program', $_POST['program'] );

    I want to filter all my authors that have a matching field of “Rome, Italy”, I can get all the authors to show and then all the locations to show but how do I get it to filter. Here is my beginning code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 15 replies - 1 through 15 (of 15 total)
  • by custom field key:

    $query = new WP_Query( 'meta_key=color' );

    by custom field value:

    $query = new WP_Query( 'meta_value=blue' );


    $query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );

    I still need help trying to filter on some fields I have in user profile, here is my code to pull all the authors who have a post:

    $author_count = array();
    	foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
    		$author_count[$row->post_author] = $row->count;

    how do I adjust this so that I can pull only authors who have “Rome, Italy in their Location field. I am not a very strong SQL coder so any help would be most appreciated.

    You might try:

    $args = array(
        'orderby' => 'title',
        'order' => 'ASC',
        'meta_query' => array(
            array  (
                'key' => 'Rome,italy',
    query_posts( '$args' );

    Ok so it does look like this is outputting something in the source:

    <div class="author" id="author-">
    			<a href="">
    			<span class="read-blog">Read</span>
    						<ul class="info">
    				<li class="name"></li>

    But I am not sure how to use $args to output all the different elements I need to be displayed, here is my code the that works for displaying all users:

    [36 lines of code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    How do I use that filter to output all the elements, even just listing one would help me. Thanks for all your help

    Actually I just tried to remove all my code and just use your code, and nothing is actually pulling in the source, before I had my old code in there as well so I must have been pulling the admin account so nothing is actually pulling

    basically, to this sql line I need to somehow add the location field that is in the User profile to filter on Rome, Italy:

    			$excluded = '824, 265, 9, 1435, 1372, 920, 1437, 1695'; // To exclude Admin accounts
    			$sql = 'SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE post_author NOT IN ($excluded) ORDER BY post_date ASC";
    			$authors = $wpdb->get_results($sql);
    			foreach($authors as $author):

    I’m sorry, I’m just not clear on what you’re doing. You want to add a feild? or are you trying to add args to a query?

    What exactly are you trying to output? in layman’s terms..

    Basically what I am trying to do is output a list of bloggers based on country. This country is a field they indicate in their User Profile (i.e. Rome, Italy). I currently can list all the bloggers and their locations but I was to build a page for say “Rome, Italy” and only show the bloggers who put that in their ‘location’ field. Does that describe what I am trying to do? Thanks for helping me out.

    I’m not sure how you are supplying the values, is it at sign up? Are users creating their own custom feilds?

    If you use the query I posted it should pull posts with custom feild values of Rome and Italy. It’s supposed to be a multiple value query, but I didn’r test it.

    Every user has a field in their actual profile called “location” where they indicate where they are traveling to, here is the blog:

    Every blogger has a country (i.e. location) they indicate in the user profile, so it is not associated with a post but their user profile. So trying to figure out how to filter by the field that matches say “Rome, Italy”, does that clarify?

    You can see all the bloggers here,, I want the same layout but just filter it by country that matches “Rome, Italy”

    So, you are trying to query by profile feilds? I’ve never done that.
    Let me think on it.

    Yes, here are the fields in my functions.php file:

    update_usermeta( $user_id, 'location', $_POST['location'] );
    update_usermeta( $user_id, 'program', $_POST['program'] );

    this may help:


    If you can get the profile feild values automatically as custom feilds, those feild values can be easily queried.

    Although, if there are no posts involved…? I’m not sure how it would be done.

    I saw the site, so you want all profiles to appear ordered by location, and the location must be pulled from a user profile feild.

    You might do the same with profile groups, like in buddypress. Buddy press allows you to create multiple profile groups, selectable and filled by user from their profile. you could have one for each location?

    nah, thats too many loacations. hmmm

    check this out:

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Filter by User Custom Field’ is closed to new replies.