I am making a new WP_Query to search for a product.
This shows me the wp_post information but not the stock information.
How can I order the stock results on top and the non-stock ones last in the search.
This is my current code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );
Try this code but it doesn't work
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => '_stock',
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );
Related
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'
)
);
I want to get some products of an post_author from DB. The problem is that get_posts is not working properly, from my debug that's a problem from the theme that I am using.
Is there any alternative to get products from DB?
What I tried:
-----1----
$related_products = get_posts( array(
'post_type' => 'product',
'author' => 19,
'post_status' => 'publish',
) );
-----2----
$product_query = new WP_Query( array(
'author' => 19,
'post_type' => 'product',
'post_status' => 'publish',
) );
$related_products = $product_query->posts;
-----3----
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'product',
'author' => 19,
) );
$GLOBALS['wp_query'] = new WP_Query();
$related_products = $GLOBALS['wp_query']->query( $args );
I get results, but random products.
I was searching for this but didn't find the solution. I am getting posts orderby title but some posts need to come first so I added a custom field which meta_key is display_postion and the values for this fields are numbers i.e. 1,2,3. So how do I achieve this, below is my current code.
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby'=> 'title',
'post_status' => 'publish',
'posts_per_page' => -1,
'paged' => $paged
;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts()) : $wp_query->the_post();
Adding meta_value_num to an orderby array and referencing 'display_position' in the meta_key field should do the trick:
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby' => array(
'meta_value_num' => 'ASC',
'title' => 'ASC'
),
'post_status' => 'publish',
'posts_per_page' => - 1,
'paged' => $paged,
'meta_key' => 'display_position'
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()):
while ($wp_query->have_posts()):
$wp_query->the_post();
See documentation for Order and Orderby parameters for WP_Query.
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 am trying to make a simple query in wordpress ordered by a meta_value_num of a custom field.
$args1 = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
I want to include the posts that have the custom field empty or null too ordered first or last in the query. How can I achieve that?
A simple and quick solution:
<?php
setup_postdata( $GLOBALS['post'] =& $post );
$posts= get_posts(array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
));
if( $posts ):
foreach( $posts as $post ): ?>
<!-- HTML HERE -->
<?php endforeach;
wp_reset_postdata();
endif; ?>
?>
If you want to get the generated SQL just pass it to the WP_Meta_Query object:
$query_args = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
$meta_query = new WP_Meta_Query();
$meta_query->parse_query_vars( $query_args );
$res = $meta_query->get_sql(
'task',
$wpdb->posts,
'ID',
null
);