Ordering Wordpress posts with meta values - wordpress

I have this code below that basically creates 4 links to allow me to sort posts on the front end.
<div class="sort">
Sort projects by:
<a href="http://mydomain.com/find-work/" >Latest Projects</a>
<a href="http://mydomain.com/find-work/?order=asc&orderby=date" >Ending Soon</a>
<a href="http://mydomain.com/find-work/?order=asc&orderby=meta_value_num&meta_key=proj_budget" >Budget Low</a>
<a href="http://mydomain.com/find-work/?order=desc&orderby=meta_value_num&meta_key=proj_budget" >Budget High</a>
</div>
<?php $my_query = new WP_Query( array(
'post_type' => 'project',
'orderby' => get_query_var('orderby'),
'order' => get_query_var('order'),
));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
The second link, ordering by date works fine but the two links to order by meta values is not working. I am obviously missing something in my query but for the life of me can't work it out.
Any ideas??

It's a bit magical with meta values:
$my_query = new WP_Query( array(
// 'post_type' => 'project',
'meta_key' => 'proj_budget',
'orderby' => 'meta_value_num'
));
All the possible values are explained in codex: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

It's quite simple:
new WP_Query( array(
//I used meta_value_num below, because it's about a numeric field
//if you don't have a numeric field, just use meta_value
"orderby" => 'meta_value_num',
"meta_key" => 'price',
"order" => 'DESC'
));

Related

Exclude Current and Sticky Post

On a single Post page I have a side bar displaying up to three other, related posts. How can I exclude both Sticky Posts and the Current post?
I know how to exclude the Current post and how to exclude Sticky Posts by using post_not_in in a WP_Query, see code example below. But I guess you can not use post__not_in twice in the same query. Any suggestions?
$current_post_ID = get_the_ID();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => get_option( 'sticky_posts' )
'post__not_in' => array($current_post_ID)
);
The post__not_in is an array() and uses post ids (numbers only).
There is no need to use it twice, use it like that:
//First build the array of excluded posts
$excluded_posts = array_push(get_option( 'sticky_posts' ) , $current_post_ID);
//Then use it in the option
'post__not_in' => array( $excluded_posts )
Note the array_push() PHP function, this will add the current post to the end of the sticky posts array, which is then passed to 'post__not_in' option.
<?php
$sticky =array(get_option('sticky_posts'));
// (add post id on sticky_posts option like ex. 485,458,256)
$current_post_ID = get_the_ID();
array_push($sticky,$current_post_ID);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => array($sticky)
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
<!-- add your code which to display hear -->
<?php
endwhile;
wp_reset_query();
?>

Wordpress ordering posts by meta key

This has been confusing me no end.
I have this query on a page, works fine to order posts by meta_value.
query_posts( array( 'meta_key' => 'epicredrank', 'orderby' => 'meta_value_num', 'order' => 'DESC' , 'paged' => $paged, ) );
Trying to create a link to order the posts like this and it isn't working.
<a class="voted-on" href="<?php bloginfo('url'); ?>?meta_key=epicredrank&orderby=meta_value&order=DESC">
<span>Hot Posts</span>
</a>
It doesn't re-order anything, the query, when passed to the URL seems to do nothing at all.
What am I doing wrong?
Edit- Want to sort posts by meta value via a link.
Try this
<a class="voted-on" href="<?php bloginfo('url'); ?>?meta_key=epicredrank&orderby=meta_value_num&order=DESC"><span>Hot Posts</span></a>
$meta_key = mysql_real_escape_string((isset($_GET['meta_key']) ? $_GET['meta_key'] : 'epicredrank' ));
$order_by = mysql_real_escape_string((isset($_GET['orderby']) ? $_GET['orderby'] : 'meta_value_num' ));
$order = mysql_real_escape_string((isset($_GET['order']) ? $_GET['order'] : 'DESC'));
query_posts( array( 'meta_key' => $meta_key, 'orderby' => $order_by, 'order' => $order , 'paged' => $paged ) );

Changing Wordpress post order with URL string

I am trying to add a dropdown to sort my custom posts.
I have tried the solutions here - http://ak.net84.net/php/filter-dropdown-for-wordpress/ - and here - http://blog.rutwick.com/use-jquery-to-reorder-your-wp-posts-on-the-fly
I can’t get either of these to work and I can’t even get my posts to sort by adding this to the end of my URL - ?orderby=title&order=DESC.
Out of curiosity, I went over to DigWP and tried this - http://digwp.com/category/admin/?orderby=title&order=DESC which worked and sorts the posts by title and in descending order.
So I am wondering why it won’t work on my site? Here is the code that is getting my posts.
<?php $my_query = new WP_Query( array(
'post_type' => 'project',
'post_status' => 'publish',
'paged' => get_query_var('paged'),
));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
I'm guessing there is something wrong with the query or perhaps wp_query won't allow ordering posts in this way?
Any help appreciated.
If you want to get the query string variable, i.e. ?orderby=title
$my_query = new WP_Query( array(
'post_type' => 'project',
'post_status' => 'publish',
'orderby' => get_query_var('orderby'), // will return orderby query string variable
'order' => 'DESC',
'paged' => get_query_var('paged'),
));

WordPress query_posts display past dates

I have my page displaying only future dates which is just what I want, I have been digging around trying to find a way to query only: posts that are dated prior to todays date.
This is currently my query:
<?php
$args=array(
'post_type' => 'artists',
'orderby' => 'ecpt_featured_month',
'order' => 'ASC',
'post_status' => 'future'
);
Any ideas? If it helps, each post is an artists and they have a featured month, post = month.
Thanks,
Ryan
Use this one considering "ecpt_featured_month" is a custom field:
$args=array(
'post_type' => 'artists',
'orderby' => 'meta_value',
'meta_key' => 'ecpt_featured_month'
'order' => 'ASC',
'post_status' => 'future'
);
$loop = new WP_Query( $args);
while ( $loop->have_posts() ) : $loop->the_post();
// write the general code to display the post's title and date
endwhile;

Wordpress custom field date, display post with custom date > current in ascending order

How do I order post by a custom field if "releasedate" > "currentdate" but in acending order. Basically only display post with dates starting after today but in ascending order. currently i have
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=10&post_type=movies&meta_key=releasedate_value&orderby=releasedate_value&order=ASC');
if (have_posts()) : while (have_posts()) : the_post();
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
?>
<?php if ($releasedate > $currentdate) {?>
"my contents/ post here"
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
now everything works except when its ASC and not DSC, no post will display because wordpress in getting the post first and the erasing the post that are not before current date and only 10 post are allowed, therefore if 10 post has a release date before today, wordpress loads then erase them after and leaves everything blank! Thank you for your help
I'd put the date criteria into the query itself. Assuming you have WordPress version 3.1 or higher, you can use the meta_query parameter. Something like:
<?php
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$wp_query = new WP_Query( array ('showposts' => 10,
'post_type' => 'movies',
'meta_query'=> array(
array(
'key' => 'releasedate_value',
'compare' => '>',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'releasedate_value',
'orderby' => 'meta_value',
'order' => 'ASC'
)
);
should work.
This works with me too:
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$args = array(
'post_type' => 'event',
'meta_key' => 'date_field',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_field',
'value' => $currentdate,
'compare' => '>=',
'type' => 'DATE'
),
),
);
$my_query = null;
$my_query = new WP_Query($args);

Resources