Ordering WP_Query by 'meta_value_num' while also using 'offset' - wordpress

I have a WP_Query like so:
<?php $args = array(
'post_type'=> array('post', 'page'),
'cat'=> 108,
'posts_per_page' => 10,
'offset' => 4,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'after' => '3 months ago'
)
),
);
query_posts( $args ); ?>
What I want is are 10 posts that:
Have the highest value for post_views_count
Are not the 4 most recent posts published.
But it seems that 'offset' => 4 is running the query, ordering by post_views_count and then skipping 4 of those, instead of the most recently published 4.
Would anyone have any insight into how I can it to skip the first 4 and then run my meta_key query?

If you get somewhere earlier 4 first published posts you must put their ids in array and use query arg
'post__not_in' => $exclude_posts_arr
instead of
'offset' => 4,
If you didn't get this post earlier you can create another query with this parameters.

Related

Sort by custom meta_key in WordPress get_posts(), and include posts that do not have the meta_key

I have set on my WordPress post custom meta called "my_sort" which have a numeric values like 1, 2, 3, 4..... See below image:
I want to sort in ascending order by this meta "my_sort". This is the below code I am applying:
<?php
$catPost = get_posts(array('category' => get_cat_ID($categories[0]->name), 'meta_key' => 'my_sort', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'numberposts' => 100)); //change this
?>
The problem with this code is that it is working but has one problem which I need to be fixed.
It is leaving all the other posts which do not have "my_sort" meta in them. I want to include those posts also. I want:
First the posts that have "my_sort" must come in ascending order.
Then the post that do not have "my_sort" meta should come.
Update
When I try the below query.
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);
I do not get proper result. See the image which shows the result.
Here:
Record1 : does not have "my_sort" meta.
Record2 : does have "my_sort" and it's value is 2.
Record3 : does not have "my_sort" meta.
Record4 : does have "my_sort" meta and it's value is 4.
The result in this case should be like this:
Record2 then Record4 then Record1 then Record4 but clearly this is not the case.
What is wrong?
If you want to sort by a meta key that might not have any values, you need to use a meta_query that combines the results of a search for posts with the key and a search for posts without the key.
This isn't tested, but the main logic is there:
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);

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.

Woocommerce - Custom sorting and then ABC order

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

Wordpress loop ignore newest post in query

I'm trying to pull out all of the posts that have a meta value of Main, while trying to ignore the newest post created. I've looked here (WordPress site https://codex.wordpress.org/Class_Reference/WP_Query) but I can't figure out a way to ignore the newest post. It can't be by a set date, because it needs to always ignore the newest post.
$args = array(
'order' => 'DESC',
'meta_key' => 'main_story',
'meta_value' => 'Main',
'meta_query' => array(
'relation' => 'NOT IN',
array(
'key' => 'main_story',
'value' => Main,
'posts_per_page' => 1,
'order' => 'DESC',
),
)
);
I thought trying it this way not not get the newest post, but it hasn't worked. Is there an opposite of getting the meta_query, like ignore_meta_query, but you know, that works?
There are a lot of issues with your posted code...but you're looking for the offset parameter of WP_Query.
offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The offset parameter is ignored when 'posts_per_page'=> -1 is used.
$args = array(
'offset' => 1,
'posts_per_page' => 1,
'meta_key' => 'main_story',
'meta_value' => 'Main',
'meta_compare' => 'NOT',
);

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

Resources