query post by author gender - php

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

Change this:
<?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',
);
For this:
<?php $custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'gender',
'meta_value' => 'Homme',
'order' => 'DESC',
'orderby' => 'date',
);

Related

wordpress order by multiple

I have a query in wordpress that looks like this,
$args = array(
'post_type' => 'our-team',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => array(
'date_published' => 'ASC',
)
);
I am wanting to order my results by 2 attributes, firstly by date_published and then secondly my a meta value "weight". Weight is a numeric value (1 or 2).
When I change it the query to be,
$args = array(
'post_type' => 'our-team',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'weight',
'orderby' => array(
'date_published' => 'ASC',
'meta_value' => 'ASC'
)
);
When I run this query it only returns posts that have a weight of 1?
$args = array(
'post_type' => 'our-team',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'weight',
'orderby' => array( 'meta_value_num' => 'ASC', 'post_date' => 'ASC' )
);
Use date instead of date_published i think date_published is not the right key. and weight should be in int so better to use meta_value_num instead of meta_value try the below code.
$args = array(
'post_type' => 'our-team',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'weight',
'orderby' => array(
'date' => 'ASC'
'meta_value_num' => 'ASC'
)
);

Combine Two PHP/MySQL queries

I'd like to combine two php/mysql queries against the wp_posts table using the code below.
However, as you can see, there's an error there with the second post_type. The second post_type is 'post', and is never recognized. :-(
How would I make that an "AND", to gather BOTH the 'membercontent' and 'post' data ? The same question applies to the 'value' statement just below it. 'true' and 'yes'.
<?php $args = array(
'post_type' => 'membercontent', 'post',
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => 'true', 'yes',
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
`$ourposts = new WP_Query( $args );?>
Just figured it out. Duh. Had the answer all along....ARRAYS!
<?php $args = array(
'post_type' => array ('membercontent', 'post',),
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => array ('true', 'yes',),
),
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>

WP_Query filtering

So I've been looking around here and other sites for a solution. I found lots of really helpfull posts but for some reason I just cant get this to work.
What I have:
WP posts with custom fields.
One is "rating" which is given a number between 1-5
The other is "flash" with either a 1 or a 0.
What I want to do:
Show all posts with a 1 on flash, in ORDER descending by the "rating"...
I currently have:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'meta_key' => 'flash',
'meta_value' => '1',
)
);
$ultimos = new WP_Query( $args );
This does NOT filter the flash custom field.
however if I do:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'flash',
'meta_value' => '1',
);
$ultimos = new WP_Query( $args );
This DOES filter flash, but does not order them properly.
Any thoughts?
I believe you need to take a look at using the relationship feature of the WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'flash',
'value' => '1',
'compare' => 'LIKE',
),
);
$ultimos = new WP_Query( $args );

Exclude posts from get_posts()

Good morning, I found many similar questions, but none of the answer fit to my problem. The point is very simple: I have a custom loop with get_posts(), and I want to exclude current post from being displayed.
The code is:
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'post__not_in' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
I tried with:
'post__not_in' => array(get_the_ID),
'post__not_in' => array($post->ID),
'exclude' => $post->ID,
'exclude' => get_the_ID,
and with many other combinations with or without array. Of curse, current post id is correctly echoed before this loop, and if I try echo($post->ID) and echo(get_the_ID()) I have the same, correct, result.
I really don't know what's happening,
thank you very much for help,
Marco
Try exclude.
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'exclude' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
here is a function that does just that:
function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;
}
Usage: say my category id is 22 then:
$last_post_ID = get_lastest_post_of_category(22);
you can also pass an array of categories to this function.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 18,
'paged' => $paged,
'offset' => 0,
'post__not_in' => array($last_post_ID,),
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
// The Query
$the_query = new WP_Query( $args );

get posts equal to two meta_values

I'm trying to retrieve all posts where the meta_key status is equal to either correct or wrong. at the moment i've created below php variable however it shows all posts also posts where the meta_key is not equal to correct or wrong. What am i doing wrong?
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'cat' => '4,5,6',
'meta_query' => array(
array(
'meta_key' => 'status',
'meta_value' => 'wrong'
),
array(
'meta_key' => 'status',
'meta_value' => 'correct'
)
)
);
$the_query = new WP_Query( $args );
Try this:
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'cat' => '4,5,6',
'meta_query' => array(
array(
'meta_key' => 'status',
'meta_value' => array('wrong','correct')
'compare' => 'IN'
)
)
);

Categories