WP_Query filtering - php

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 );

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'
)
);

wp_query and sorting on meta data excludes products with no entries

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'
);

query post by author gender

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',
);

show pinned item at the very top of posts

I have created a checkbox with the help of metabox for pinning news items, just can't keep last pinned item be at the very top of list, here is my code:
$today = date("Ymd");
$args = array(
'post_type' => 'news',
'posts_per_page' => -1,
'meta_key' => 'pinned_news_item',
'meta_value' => '1',
'order' => 'DESC',
'orderby' => $today,
);
Finally found a solution. Here is a code that worked for me:
$args_meta = array(
'post_type' => 'news',
'posts_per_page' => -1,
'meta_key' => 'pinned_news_item',
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1'
);
Please try by editing your query:
$args = array(
'post_type' => 'news',
'posts_per_page' => -1,
meta_query' => array(
array(
'key' => 'pinned_news_item',
'value' => '1',
)
),
'order' => 'DESC',
'orderby' => $today,
);
Reference: http://www.happiweb.net/2015/05/code-wordpress-thuong-dung.html

Wordpress Query Args - Only show post with meta_value greater than 0

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' => '>'
)
)
);

Categories