Support » Plugin: Buddypress Xprofile Custom Fields Type » Get posts by xprofile field

  • Resolved applegateian


    Hi guys

    All of the users on my site will be required to choose a custom xprofile type called ‘account type’ when they register.

    There are 5 different types.

    Each user can create standard WordPress posts that appear on a top level page called ‘posts’.

    I need to filter ‘posts’ further, by these 5 user types to run 5 separate WordPress pages. These would be posts by these authors, filtered by their different xprofile paths.

    How can I query by the Xprofile field?

    Many thanks,


Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Miguel López


    I’ve been looking for an answer and I don’t know one easy…

    For now, if I were in your situation, I would filter first the authors using something like BP Profile Search plugin In this plugin, we can search members looking in xprofiles fields.

    With this, we obtain the user_id of the authors and you can use it to make a query of posts written by these authors. Maybe there is another way but I couldn’t find it.

    Try this it just might work! if i have understood you correctly!

    1) put this into functions.php or ……

    function include_by_meta($theMetaValue, $theMetaField) {
    	$memberArray = array();
    global $members_template;
    		if (bp_has_members()) :
    		while (bp_members()) :
    			$theFieldValue = bp_get_member_profile_data( 'field='. $theMetaField );
    			if ($theFieldValue==$theMetaValue) {
    				array_push($memberArray, bp_get_member_user_id());
    	return $theIncludeString;

    2) Make your pages —- copy members-loop into each page account_type_1, account_type_2, etc


    begin each page

    <?php do_action( 'bp_before_members_loop' ); ?>
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>
    <?php $yvar= "account_type_1";  if (bp_has_members( array( 'include' => include_by_meta($yvar,'account_type'), 'type' => 'newest') )) : ?>

    etc etc

    don’t forget the endif

    <span class="update"> <?php bp_member_latest_update(); ?></span>
    <?php endif; ?>

    Cheers and hoping it works

    i have assumed your bp xprofile field is account_type

    obviously replace account_type_1 with account_type_2 etc

    I am looking to do something similar.

    I want to be able to filter a custom post post type with the details provided in the xprofile. The problem i have is that the data field i want to search by has many things in them.

    What im making is a custom post type of jobs advertised on the site and when they get to the site i nead to search the posts custom fields or tags with the xprofile data field.

    I hope that makes sense

    Plugin Author Miguel López


    Sorry to answer so late, I’m kind of busy lately.

    Mmm, I think I understand. For example, you can have custom post type of jobs in London and you want to show them when a user from London is logged in.

    Well, for the custom posts types you can use tags or maybe Types plugin (Great plugin!). With this code, you can get any data from profile field of the current logged in user:

    global $bp;
    $your_data = bp_get_profile_field_data('field=nameofprofilefield&user_id='.bp_loggedin_user_id());

    Next thing to do is to filter your custom posts types using this data.

    Brilliant! thank you for the reply. I’ll let you know how i get on!

    Plugin Author Miguel López


    Ok, I think this is solved now…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get posts by xprofile field’ is closed to new replies.