how to add another list parameter for meta_query - wordpress

here is my wordpress new query loop $args
if (isset($_GET['list_shows'])) {
if ($_GET['list_shows'] == 'update') {
$orderby = 'modified';
$order = '';
} elseif ($_GET['list_shows'] == 'views') {
$orderby = 'meta_value_num';
$order = 'DESC';
} elseif ($_GET['list_shows'] == 'popularity') {
$orderby = 'comment_count';
$order = 'DESC';
}
} else {
$orderby = 'modified';
$order = '';
}
$argz=array(
'posts_per_page' => '-1',
'orderby'=>$orderby,
'order'=>$order,
'meta_query' => array(
array(
'key' => 'fragman',
'compare' => 'NOT EXISTS',
'posts_per_page' => '-1',
)
),
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => '2 days ago',
),
'posts_per_page' => '-1',
),
);
the question is about ordering posts by meta_value_num , descending ?
my post views meta key = views, could we add this ordering parameter in this meta query ?
if it is, how can we do it. ?
Thanks.

You can pass like this
global $wp_query;
$args = array(
'meta_key' => 'views',
'orderby' => 'meta_value meta_value_num',
'order' => 'DESC'
);
query_posts( array_merge( $args , $wp_query->query ) );
I hope this will work for you.

Related

meta_query is not working with tax_query as expected in WP_query

SOLVED!
I have a custom post with some metabox and taxonomy. I'm trying to filter custom posts by combination of meta value and taxonomy and it's working as expected. When both meta value and taxonomy selected it's returning correct result, only taxonomy selection is also working. But if I select only meta value and try to filter post it's returning nothing. Here I'm sharing a video link of the output: https://youtu.be/XaCeJ_LcPhc. Is is possible to solve the issue without custom query?
$category_array= [];
$metabox_array = [];
foreach($_POST as $key => $value){
if($key == 'ms_product_categories'){
foreach($value as $cat_slug){
array_push( $category_array, $cat_slug);
}
}else{
array_push( $metabox_array, array('key' => $key, 'value' => $value ) );
}
}
if(count($metabox_array) >= 2){
$relation = "'relation'=>'AND'";
}else{
$relation = '';
}
$args = array(
'post_type' => 'ms_product',
'meta_query' => array(
$relation,
$metabox_array
),
'tax_query' => array(
array(
'taxonomy' => 'ms_product_categories',
'field' => 'slug',
'terms' => $category_array
),
)
);
$ms_products = new WP_Query( $args );
Here is the solution, maybe it will be helpful for someone.
$category_array= [];
$metabox_array = [];
foreach($_POST as $key => $value){
if($key == 'ms_product_categories'){
foreach($value as $cat_slug){
array_push( $category_array, $cat_slug);
}
}else{
array_push( $metabox_array, array('key' => $key, 'value' => $value ) );
}
}
if(count($metabox_array) > 1){
$metabox_array['relation'] = 'AND';
}
if(!empty($category_array)){
$tax_query = array(
'taxonomy' => 'ms_product_categories',
'field' => 'slug',
'terms' => $category_array,
'include_children' => true,
);
}else{
$tax_query = '';
}
$args = array(
'post_type' => 'ms_product',
'meta_query' => $metabox_array,
'tax_query' => array(
$tax_query
)
);

WooCommerce get all products with SKU

I want to get a list of products with sku and post id with this code, everything seems fine and ok with this code :
$statuses = array('publish', 'draft');
// Args on the main query for WC_Product_Query
$args = [
'status' => $statuses,
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ($product->get_type() == "variable") {
// Args on product variations query for a variable product using a WP_Query
$args2 = array(
'post_parent' => $product->get_id(),
'post_type' => 'product_variation',
'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ),
'fields' => 'ids',
'numberposts' => -1,
);
foreach ( get_posts( $args2 ) as $child_id ) {
// get an instance of the WC_Variation_product Object
$variation = wc_get_product( $child_id );
if ( ! $variation || ! $variation->exists() ) {
continue;
}
$list_array[] = array(
'sku' => $variation->get_sku(),
'postid' => $variation->get_id()
);
}
} else {
$list_array[] = array(
'sku' => $product->get_sku(),
'postid' => $product->get_id()
);
}
}
I have total 1660 (470 published and 1,190 drafted) products but it's just returns 501 products and i don't know why!
this is my products in woocommerce:
this is the final result of query :
this is the result of Janki's code
// Woocommerce get all products
$args = array(
'post_type' => array('product','product_variation'),
'meta_key' => '_sku',
'post_status' => array('publish','draft'),
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'EXISTS',
),
),
);
$products = new WP_Query($args);
/* using this code you can get all products */
/* this may help you to get details */

Issue with function and post ID

I am using the following function/shortcode in order to output an average global rating using ACF :
function get_average_rating($post_id) {
$rating_sum = 0;
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );
if ( empty( $reviews_of_post ) ) {
return 0;
}
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
return number_format((float)($rating_sum / count( $reviews_of_post )),1, ',', '');
}
add_shortcode( 'note-clients', 'get_average_rating');
It always return 0 except when I manually input the post ID like :
'meta_query' => array(
array(
'key' => 'produit',
'value' => 1234,
'compare' => '=',
),
),
How can I fix this ?
Thanks a lot !
Declare global $post; before your function.
Wordpress uses $post for many of its functions within the loop.
To avoid any conflicts later you should consider use of wp_reset_query
1 . Change key 'key' => 'produit' to 'key' => 'product',
2 .
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
**to**
foreach ( $reviews_of_post as $review ) {
$post_id = 'post_' . $review->ID ;
$rating_sum += get_field( 'note_client', $post_id );
}
Confirm ACF key (note_client) is correct
3 . Change this
add_shortcode( 'note-clients', 'get_average_rating');
to
add_shortcode( 'average_rating', 'get_average_rating');
Please try with your post static id:
$post_id = 9; //add here your static post id
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );

Can't add to $args $_GET value conditionally - WordPress

In WordPress I have a loop with some $args array, which I want to be able to edit conditionally depending on a $_GET value.
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'fields' => 'ids',
'no_found_rows' => true,
'post__in' => $post__in,
'nopaging' => true
);
When adding to $args array like this:
if ( $_GET['prop_sort'] == 'price' ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = $prefix . 'price';
}
Nothing changes, and added to $args stuff gets ignored. However when I put code like this:
args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'fields' => 'ids',
'no_found_rows' => true,
'post__in' => $post__in,
'nopaging' => true
);
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = $prefix . 'price';
Everything works as intended and posts are sorted by price from small to big.
What could be the problem?
EDIT:
Trying to influence query like this:
function myplugin_pre_get_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'property' ) {
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
$query->set( 'meta_key', 'sb_price' );
}
return $query;
}
add_action( 'pre_get_posts', 'myplugin_pre_get_posts', 1 );
Dones't work either (Undefined index: post_type in ..\functions.php on line 65).

Wordpress paginate_links() not working

Using Wordpress 4.0.1
I have query set up to echo out archive posts to a page. Which works fine, here is the query.
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'post',
'category__in' => array($category[0]->cat_ID), // gets the category ID higher up in the code.
'posts_per_page' => 5,
'paged' => $paged
);
if ($category[0]->cat_ID == 3) {
$today = date('Ymd');
if (!empty($year) && !empty($monthnum)) {
$args['meta_query'] = array(
array(
'key' => 'event_start',
'compare' => '<',
'value' => $today
)
);
} else {
$args['meta_query'] = array(
array(
'key' => 'event_start',
'compare' => '>',
'value' => $today
)
);
}
$args['order'] = 'ASC';
$args['orderby'] = 'meta_value';
$args['meta_key'] = 'event_start';
}
$the_query = new WP_Query( array_merge($wp_query->query, $args) );
I'm getting out the posts and then at the bottom I want a pagination feature.
Here is my pagination query
$args = array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'add_args' => true,
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => true,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'end_size' => 3,
'type' => 'list'
);
echo paginate_links($args);
But whats happening is the link at the bottom is showing the url twice like this
http://domain.com/category/recent-seminars/page/2domain.com/category/recent-seminars/page/2/?future
This results in 'Nothing Found' (unsurprising), but even if I delete either the first half or second half, I still get nothing found.
I've tried loads of different combinations in the paginate_links query but nothing is working.
EDIT
I've amended the main query to this now
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'post',
'category__in' => array($category[0]->cat_ID),
'paged' => $paged
);
$today = date('Ymd');
if ($category[0]->cat_ID == 3) {
$args['meta_key'] = 'event_start';
$args['meta_query'] = array(
array(
'key' => 'event_start',
'compare' => '>=',
'value' => $today
)
);
$args['order'] = 'ASC';
$args['orderby'] = 'meta_value_num';
}
This works for the first page, but as soon as I click one of the next page links, it stops working, however If i take out the $args['meta_query'] array, the paging does work, but the posts are in the wrong order.

Resources