I see there was a mistake in the code earlier. I’m pasting this code here:
get_header(); ?>
<?php get_template_part('page-parts/general-title-section'); ?>
<?php get_template_part('page-parts/general-before-wrap'); ?>
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'country',
'meta_value' => 'Spain'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php get_template_part('page-parts/general-after-wrap'); ?>
<?php get_footer(); ?>
Please use the code
button when posting code.
You’re querying against user_meta.
But the data is an xprofile field.
You need to collect the user ids of all members who have a certain xprofile value re the country field.
Then use the author__in
parameter in WP_Query
https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
So should I put it somehow like this?
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'authors_in => array('country=Spain')'
);
? I’m not really sure how to query against an xprofile field. Do I need to call something from the database? Or should I do it as in the example in the link you’ve provided me with?
$query = new WP_Query( array( 'author__in' => array( 'country=Spain' ) ) );
Sorry for my limited knowledge, still learning how to use wordpress and bp.
Thank you
I think what @shanebp is saying is you’ll need to collect the user IDs separately and then use that array of IDs in your query.
Okay, I’ve been trying this a bit and I still don’t get it.
I used 'who'=>'authors'
to get all the authors’ posts displayed on a page. What I still can’t get is how to extract the data from wp_bp_xprofile_data. I tried putting that in an array
$country = array (
'key'=>'country',
'value'=>'Spain');
And then putting it like this
$query = new WP_Query( array( 'author__in' => $country ) );
But still nothing. Ive found also this piece of code:
$user_query = new WP_User_Query( array( 'meta_key' => 'country', 'meta_value' => 'Israel' ) );
Which is almost the same thing I want to do just that I want to filter posts by such users. Here they’re using meta_key and meta_value again and I’ve got no idea how to do the same with wp_bp_xprofile_data.
Am I getting close or am I still far from achieving what I want?
Thank you for your help
Did you read the link re author__in
?
It uses ids, not country.
As we’ve said, it’s a two-step process.
Look at function my_custom_ids
on this codex page for an example of gathering ids from xprofile.
Then pass the ids to author__in
.
Okay, so while I’ve made some progress thanks to you two, I’m still missing something. I thought it was working tomorrow but the code wasn’t logical(I had to use author__not_in
to display posts from all Spanish users but it didn’t work with other countries).
So first of all, this code gives me only the users from a given country:
<?php $Members = array((bp_has_members( my_custom_ids( 'country', 'Spain' ) )) ); ?>
I’ve used the example Here and it displays only those two users I have with Spain. When I change to France it displays only one user, which is correct. So this one is good to go.
I’m having trouble with the rest though.
<?php
$query = new WP_Query( array( 'author__in' =>array($Members)));
if ($query->have_posts()) :
// Start the Loop.
query_posts($query);
while ($query->have_posts() ) : $query->the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'page-parts/post-content-small' );
get_template_part( 'page-parts/posts-social-share' );
?>
<?php if ( sq_option( 'page_comments', 0 ) == 1 ): ?>
<!-- Begin Comments -->
<?php comments_template( '', true ); ?>
<!-- End Comments -->
<?php endif; ?>
<?php
endwhile;
endif;
?>
Is what I’ve been trying to do. Putting (bp_has_members( my_custom_ids( 'country', 'Spain' ) ))
into an array like this:
$query = new WP_Query( array( 'author__in' =>array(bp_has_members( my_custom_ids( 'country', 'Spain' ) ))));
Didn’t give me the results I wanted. What am I doing wrong? Once when I put author__not_in
it worked but only with Spain and no other country. Now it’s not working even with Spain.
Any suggestions? What should I do? Please help.
Regards
David
Don’t use bp_has_members
.
$members = my_custom_ids( 'country', 'Spain' );
Don’t implode the array in my_custom_ids, just return the array.
$query = new WP_Query( array( 'author__in' => $members ) );
Shane, thank you for your reply.
My code now looks like this:
<?php
/**
* Template Name: Spanish
*/
get_header(); ?>
<?php get_template_part('page-parts/general-title-section'); ?>
<?php get_template_part('page-parts/general-before-wrap'); ?>
<?php $members = my_custom_ids( 'country', 'Spain' ); ?>
<?php
$query = new WP_Query( array( 'author__in' => $members ) );
if ($query->have_posts()) :
// Start the Loop.
query_posts($query);
while ($query->have_posts() ) : $query->the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'page-parts/post-content-small' );
get_template_part( 'page-parts/posts-social-share' );
?>
<?php if ( sq_option( 'page_comments', 0 ) == 1 ): ?>
<!-- Begin Comments -->
<?php comments_template( '', true ); ?>
<!-- End Comments -->
<?php endif; ?>
<?php
endwhile;
endif;
?>
<?php wp_reset_postdata()?>
<?php get_template_part('page-parts/general-after-wrap'); ?>
<?php get_footer(); ?>
When I tried doing it with only my_custom_ids
I suddenly get nothing displayed at all. Now even when I try the example code from members_loop in codex I get an error like this:
Warning: Creating default object from empty value in C:\xampp2\htdocs\wp-content\plugins\buddypress\bp-members\bp-members-template.php on line 627
Viewing 1 – 0 of 0 members
Fatal error: Call to undefined method stdClass::members() in C:\xampp2\htdocs\wp-content\plugins\buddypress\bp-members\bp-members-template.php on line 609
Sorry for prolonging this topic, this is the last feature I need to add and I thought it’d be easier.
Thank you for your help
Best
David