0

I'm using utimate member plugin on my website. for each member (author), I have a field to store the member gender. (Homme / Femme).

the data is stored in my database, inside wp_usermeta.

meta key is "gender" and meta value is either "Homme", or "Femme".

enter image description here

I'm trying to write a wp-query to display all posts from all the authors, but only "Homme" authors, and another with only "Femme".

here is my wp-query to display all the posts without filtering by gender :

<?php $custom_query_args = array(

    'post_type' => 'post', 
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'order' => 'DESC',
    'orderby' => 'date',
);
$custom_query = new WP_Query( $custom_query_args ); ?>

works fine.

Here is what I've tried so far to get only the posts from "Homme" gender, but it's not working... I think I need to add a reference to post author ID somewhere but I can't find the solution.

<?php $custom_query_args = array(

    'post_type' => 'post', 
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'meta_query'             => array(
    array(
              'key' => 'gender',
            'value' => 'Homme',
            'compare' => '='
    ),
  ),
    'order' => 'DESC',
    'orderby' => 'date',
);
$custom_query = new WP_Query( $custom_query_args ); ?>

I don't know if there's a way of doin it with the plugin itself, but i'm pretty sure it can be done with a simple wp-query.

Can anybody help me with this ?

thanks.


here is what I tried, It displays ID of author who are "Homme".

<?php
$blogusers = get_users( array( 
  'meta_key'     => 'gender',
  'meta_value'   => 'Homme',
  'meta_compare' => '='
  ) ); ?>

<?php foreach ( $blogusers as $user ) {
  echo '<span>' . esc_html( $user->ID ) . '</span>';
}

?>

works fine.

What I don't know how to do, is to store in my post query the authors ID from this users query, and then use thoses ID in my query like this

array( 'author' => '2,6,17,38' ) )

where '2,6,17,38' should be the ID's of the authors.

1

If I get it right you are storing this data in user meta. WP_Query would have no remote idea how to retrieve that, since it's only aware of post meta. The two are completely separate and there is no native way to involve one in queries for another.

Likely you will have to do this in a two step process:

  1. Use get_users() to retrieve IDs for all authors of one gender and then another.
  2. Use author__in argument of WP_Query to limit posts to such set of authors.

Working of what you got so far:

$blogusers = get_users( array( 
  'meta_key'     => 'gender',
  'meta_value'   => 'Homme',
  'meta_compare' => '=',
   'fields' => 'ID',
  ) );

$custom_query_args = array(

    'post_type' => 'post', 
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'author__in' => $blogusers,
    'order' => 'DESC',
    'orderby' => 'date',
);
$custom_query = new WP_Query( $custom_query_args ); ?>
  • thanks @Rarst, I understand your point, can you please help me with your solution ? – user2882154 Apr 21 '16 at 15:52
  • What specific parts do you have problems with? I would recommend to study documentation I linked to and update question with your progress. :) – Rarst Apr 21 '16 at 15:53
  • please take a look at my answer below, thanks a lot ! – user2882154 Apr 21 '16 at 16:03
  • It's more appropriate to edit original question if you are still working out a solution. You can retrieve just IDs using 'fields' => 'ID'' in your user query, then just pass it to the respective WP_Query argument as per my answer. – Rarst Apr 21 '16 at 16:12
  • ok thanks for your reply. but I really have troubles to combine the 2 queries. using 'fields' => 'ID', would be nice if you can help me with this ! – user2882154 Apr 21 '16 at 16:15

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.