WordPress.org

Ready to get started?Download WordPress

Forums

Filter by User Custom Field (16 posts)

  1. cabplan
    Member
    Posted 2 years ago #

    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]

  2. deepbevel
    Member
    Posted 2 years ago #

    by custom field key:

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

    by custom field value:

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

    Both:

    $query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );
  3. cabplan
    Member
    Posted 2 years ago #

    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.

  4. deepbevel
    Member
    Posted 2 years ago #

    You might try:

    <?php
    $args = array(
        'orderby' => 'title',
        'order' => 'ASC',
        'meta_query' => array(
            array  (
                'key' => 'Rome,italy',
                'value'=>'true'
            )
        )
    );
    query_posts( '$args' );
    ?>
  5. cabplan
    Member
    Posted 2 years ago #

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

    <div class="author" id="author-">
    			<a href="http://blogs.luc.edu/goglobal/author/">
    			<span class="read-blog">Read</span>
    						<ul class="info">
    				<li class="name"></li>
    						</a>
    		</div>

    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

  6. cabplan
    Member
    Posted 2 years ago #

    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

  7. cabplan
    Member
    Posted 2 years ago #

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

    <?php
    			$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);
    			if($authors):
    			foreach($authors as $author):
    			?>
  8. deepbevel
    Member
    Posted 2 years ago #

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

  9. cabplan
    Member
    Posted 2 years ago #

    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.

  10. deepbevel
    Member
    Posted 2 years ago #

    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.

  11. cabplan
    Member
    Posted 2 years ago #

    Every user has a field in their actual profile called "location" where they indicate where they are traveling to, here is the blog: http://blogs.luc.edu/goglobal

    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?

  12. cabplan
    Member
    Posted 2 years ago #

    You can see all the bloggers here, http://blogs.luc.edu/goglobal/bloggers/, I want the same layout but just filter it by country that matches "Rome, Italy"

  13. deepbevel
    Member
    Posted 2 years ago #

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

  14. cabplan
    Member
    Posted 2 years ago #

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

    update_usermeta( $user_id, 'location', $_POST['location'] );
    update_usermeta( $user_id, 'program', $_POST['program'] );
  15. deepbevel
    Member
    Posted 2 years ago #

    this may help:

    user-profile-meta-value-as-custom-field

    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.

  16. deepbevel
    Member
    Posted 2 years ago #

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

    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:
    list-of-users-based-on-usermeta?

Topic Closed

This topic has been closed to new replies.

About this Topic