Woocommerce - Custom sorting and then ABC order - wordpress

I have code that I am modifying inside of Up-Sells.php
I want to order by using a custom meta (OrderForProduct) and then I want to sort by ABC order. Does anyone know how to do this?
Right now when I add the meta query sorting, the products do not show if they do not have this custom value. I am looking to have it sort by the custom value and then have it sort by ABC order.
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'meta_key' => 'OrderForProduct',
'posts_per_page' => 50,
'orderby' => 'meta_value_num', 'order'=>'ASC',
'post__in' => $upsells,
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query
);
$products = new WP_Query( $args );

What does "ABC order" mean? Are you talking about sorting by the product title? If so, I think you can do this:
'orderby' => 'meta_value_num title',
Although I've never tried it, that's how I understand it from the docos.
http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

Related

Use 'LIKE %word%' for ACF Query Wordpress

I use ACF plugin on my wordpress site and I want to code custom research.
I have many products and I want to display products which contain the searched word.
I do my query like this :
$args = array(
'post_type' => 'product',
'meta_key' => 'brand',
'meta_value' => $word,
'compare' => 'LIKE');
$the_query = new WP_Query($args);
But this display only products with the brand which exactly matches with $word.
for exemple if I search "yan" I want to display products with the brand "YANMAR", "POLYAN", "TRYANPO", etc.
How to do this please ?
Thank you and have a good day !
Try below code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'brand',
'value' => $word,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query($args);

Wordpress output links to previous and next posts from custom query

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

WordPress meta_query for Custom Post Type with orderby

I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.
The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.
The result is, glamrock gets excluded, but stickies are not sorted.
If I delete the meta_query, sorting works fine, but glamrock posts are included.
$bpb_args = array(
'numberposts' => -1,
'post_type' => 'events',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged', true ),
'meta-key' => 'sticky',
'meta_query' => array(
array(
'key' => 'glamrock',
'value' => 'no',
'compare' => 'IN',
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$bpb_query = new WP_Query( $bpb_args );
if( $bpb_query->have_posts() ):
while( $bpb_query->have_posts() ) : $bpb_query->the_post();
//show post
endwhile;
endif;
Update:
Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.
The Advanced Custom Field type is Radio Buttons.
In addition to the arguments, I also included the loop.
You need to specify only meta_value since your meta-key is non numeric
'orderby' => 'meta_value'
#Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.
Here's the query/loop that worked:
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'sticky',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'lizenz_erteilt',
'value' => 'no',
'compare' => 'LIKE',
),
)
);
$query = new WP_Query( $args );
// Loop
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
//show post
endwhile;
endif;

Wordpress Query - Order by meta_value_num AND date

im a bit stuck at the moment with the order in my wp_query.
I want to sort a query firstly by meta_value_num (this works perfect) AND as fallback by the date. But in my query the date seems dominant in comparision with the meta_value_num.
So it sorts all my posts by date and then apllys the meta_num_value order and not vice versa.
Do you have any clue how to do this?
$args = array(
'post_type' => 'anbieter',
'showposts' => -1,
'order' => 'DESC',
'orderby' => 'meta_value_num date',
'meta_key' => 'rating'
);
I found so many threads to order by two custom fields but not to sort by by "normal post field" AND custom field.
regards,
I found a solution a while ago which was this. Add this to your functions.php:
function wdw_query_orderby_postmeta_date( $orderby ){
$new_orderby = str_replace( "wp_postmeta.meta_value", "STR_TO_DATE(wp_postmeta.meta_value, '%d-%m-%Y')", $orderby );
return $new_orderby;
}
Then do this with your query:
add_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);
$args = array(
'post_type' => 'anbieter',
'showposts' => -1,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'rating'
);
remove_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);

Obtain post by id without order in wordpress

I am try to get this page by ids like this.
$args = array(
'post_type' => 'page',
'post__in' => array(2,1220,3731,696,1899,380)
);
$query = query_posts($args);
this is ok, but I obtain the post by order first 3731 and last 2, buy I don't want any order I want this order, 2, 1220, 3731, 696... etc
any idea!!
add the orderby parameter
$args = array(
'post_type' => 'page',
'post__in' => array(2,1220,3731,696,1899,380)
'orderby' => 'post__in',
);
$query = query_posts($args);
CODEX
Hi #Mark thanks for all I try this too but no work.
Finally I found the solution!
$args = array(
'post_type' => 'page',
'meta_key' => 'ordenacion',
'orderby' => 'meta_value',
'order'=>'ASC',
'post__in' => array(2,1220,3731,696,1899,380)
);
$query = query_posts($args);
I create a field custom and order by the value of meta_key

Resources