Changing Wordpress post order with URL string - wordpress

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

Related

Merge multiple WP_Query results into an array with no duplicates

I have a custom post type with its own custom template, and I want to display blog posts that are related to the custom post type title. However, I want to display a referenced post first, then search for any posts with tags, and then find relevant posts after that. Any one of these could result in no results or many. The total number of posts I want to display is 6.
`
$searchtag = strtolower(get_the_title());
$arg1 = array(
'post_type' => 'post',
'p' => $post_id,
);
$arg2 = array(
'post_type' => 'post',
'tag' => $searchtag,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => 6,
);
$arg3 = array(
'post_type' => 'post',
's' => $searchtag,
'post_status' => 'publish',
'orderby' => 'relevance',
'posts_per_page' => 6,
);
// Get the posts
$query1 = new wp_query( $arg1 );
$query2 = new wp_query( $arg2 );
$query3 = new wp_query( $arg3 );
$related_query = new wp_query();
// Merge the unique posts
$related_query->posts = array_merge($query1->posts, $query2->posts, $query3->posts);
$related_query->post_count = $query1->post_count + $query2->post_count + $query3->post_count;
if($related_query->have_posts):
`
I read an article that stated that using post__not_in was taxing on the database and I should not incorporate it.
Do I need to add logic if the query->post_count = 0?
How can I maintain the order but ensure that there are no duplicates displayed?
I get duplicates in these results currently. I'd like to eliminate them while keeping the order of the posts by query1, then query2 then query3... displaying the first 6 posts.
The better way is make 3 queries with parameter:
'fields' => 'ids'
and there:
$postIds1 = get_posts($args1);
when merge got data:
$postIds = array_merge(array(0), $postIds1, $postIds2, $postIds3);
and when make forth request with
'post__in' => $postIds

get_posts not returning all posts

I have to mount the blog posts manually, but I'm not sure if this is the correct way to work, It only brings 9 pages, with 4 posts each, but the blog has 83 posts!
<?php
$paged = get_query_var('paged');
$args = array(
'numberposts' => 4,
'offset' => $paged*4,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
?>
Thanks anyway.
Problem is your 'numberposts' is set to 4
Put it at -1 to get all posts:
'numberposts' => -1,
If you don't set numberposts here, WordPress will pull the number of posts from your Dashboard settings (under Settings -> Reading)
The below note is from this codex section.
Note: With use of the offset, the above query should be used only on a
category that has more than one post in it, otherwise there'll be no
output.
So in-order to display all posts, there should be at-least 2 posts in each categories.
You can try Loops to get all posts. Check The Loop in Action also.

Ordering Wordpress posts with meta values

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

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;

Custom Wordpress query using WP_Query()

I'm trying to get a custom query going in wordpress.
Basically, I want to select all custom post types where the variable "name" has been set to "sean".
I have tried the following :
$my_loop = new WP_Query( array( 'post_type' => 'my_post', 'meta_value=sean',
'posts_per_page' => 15, 'orderby' => 'id', 'order' => 'DESC' ) );
I got this from the wordpress codex :
Display posts where the custom field value is 'blue', regardless of the custom field key:
$query = new WP_Query( 'meta_value=blue' );
Any Help would be appreciated
EDIT: I should add that I do indeed have a wordpress loop using :
while ( $my_loop->have_posts() ) {
$pdf_loop->the_post();.... etc
Thanks again,
Dave
You are mixing query string and array style arguments. Try either
new WP_Query(array(
'post_type' => 'my_post',
'meta_value' => 'sean',
'posts_per_page' => 15,
'orderby' => 'id',
'order' => 'DESC'
));
Or
new WP_Query('post_type=my_post&meta_value=sean&posts_per_page=15&orderby=id&order=DESC');

Resources