Obtain post by id without order in wordpress - 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

Related

Wordpress wp_query exclude word and category

I am trying to exclude word 'Vape' and exclude category using wp_query. Is it possible?
$args = array(
's' => '-Vape',
'cat' => '-62',
'posts_per_page'=>'16',
'paged'=>$paged,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
I use the code above but its not working. I don't get the items that have in their title the word 'Vape' which is right but i get the items of the category 62...any help appreciated!
Try with category__not_in:
$args = array(
's' => '-Vape',
'category__not_in' => array( 62 ),
'posts_per_page'=>'16',
'paged'=>$paged,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
Edit: category__not_in expects an Array to be passed.

Get 5 random posts within current category in WordPress

I want to only get 5 posts within current category by random using the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
) );
How do you apply the '5 posts' limit and 'order by random' to the above code?
as for me, I would use get_posts(), but these arguments should work in your case as well:
<?php $args = array(
'numberposts' => 5,
'category' => $categories[0]->term_id,
'orderby' => 'rand',
'exclude' => array( get_the_ID() ),
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
More about this here: https://codex.wordpress.org/Template_Tags/get_posts
I used the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'orderby' => 'rand',
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => 3,
) );

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

To display the post order by modified date in wordpress

I have to display the posts oder by last modified date. So I used the code bellow.
$args = array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
'order'=> 'ASC',
);
$the_query = new WP_Query( $args );
But I could not find any update in the above code. Should i use something else instead of 'orderby' => 'modified' in the argument.
You should use DESC for order.
Try this:
$the_query = new WP_Query( array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
'order' => 'DESC',
));
Using DESC will give you the latest post first(descending order).
EDIT:
As Andrew commented, the default value for order is DESC and can thus be omitted from the code:
$the_query = new WP_Query( array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
));
Try
<?php query_posts($query_string . '&post_type=$post_type&orderby=modified&order=desc'); ?>

Resources