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'
)
);
Related
I'm using wp_query and sorting the results with a meta key, the problem is it's excluding products with nothing entered in the column I'm sorting on. I need all the products including ones not rated with top rated at the top.
else if($field == "rating" ) {
$myArgs = array(
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'include' => '',
'meta_key' => 'rating',
'post_type' => 'job_listing',
'post_status' => 'publish',
'job_listing_region' => 'London',
'compare' => ''
);
}
UPDATE: turns out I wsn't getting the sort results I needed, changed the code to the following but the results are still exclusive of non rated products.
$myArgs = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_type' => 'job_listing',
'post_status' => 'publish',
'job_listing_region' => "'" . $city . "'",
'meta_query' => array(
array(
'key' => 'rating',
'compare' => '<'
),
)
);
Please try this-
$myArgs = array(
'post_type' => 'job_listing',
'posts_per_page' => -1,
'meta_key' => 'rating',
'orderby' => 'meta_value',
'order' => 'DESC',
'post_status' => 'publish',
'job_listing_region' => 'London'
);
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 );
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',
);
I have a wordpress loop with an array of arguments to show only specific posts (any posts with a deposit_amount value of 0).
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0
);
$loop = new WP_Query( $args );
?>
I would like to create a similar array but showing posts with a deposit_amount meta_value of greater than 0
i have tried to use the php greater than operator but breaks the code.
'meta_value' => >0
Can anyone point me in the right direction with this problem?
just found 'meta_compare' => '>'
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0,
'meta_compare' => '>'
);
$loop = new WP_Query( $args );
?>
Use a Meta Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_query' => array(
array(
'key' => 'deposit_amount',
'value' => 0,
'compare' => '>'
)
)
);
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'
)
)
);