How to create a query that set some elements at the end of the returned array
-
I need to do a query ( with query_posts or wp_query ) that set some elements at the end of the posts array.
For example i have these elements:
1) blue
2) red
3) green
4) red
5) green
6) green
7) blue
8) green
9) blue
10) red
11) red
12) blueAnd im looking for a query that set on this way: ( ex. last elements: red )
1) blue
2) green
3) green
4) green
5) blue
6) green
7) blue
8) blue
9) red
10) red
11) red
12) redWhat should i do? Thanks for all!
-
?
do you mean that you want to fill any ‘loop’ with default (?) elements so that you always have a fixed number of ‘posts’ ?and what is the default element supposed to be? another post? always the same? just html?
can you paste your existing the code into a http://pastebin.com/ and post the link to it here?
Try it with two (multiple) loops: http://codex.wordpress.org/The_Loop#Multiple_Loops
I explain it better:
I have a real estate agency. This agency has “property sold”, “unsold properties” and “property booked”. These elements are just meta fields of a custom post type. When an user search for some property, the result must show first “unsold properties” and “property booked” custom posts and in the end “property sold” custom posts. I forgot to tell you that i also use pagination, since there are a lots of property.
Understood now? 😉
Maybe something like this: http://pastebin.com/m9xpfVB1
It first get’s the posts for “unsold” and “booked” and then get’s the posts for “sold” and then merges both.Maybe something like this: http://pastebin.com/m9xpfVB1
It first get’s the posts for “unsold” and “booked” and then get’s the posts for “sold” and then merges both.Will this works with pagination too?
Because i use this query ( for now ):$args=array( 'post_type' => $type, 'paged' => $paged, 'posts_per_page' => 5, 'orderby' => 'meta_value', 'meta_key' => 'Stato', 'order' => 'ASC' );
Thanks anyway 😉
Try it with this.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args=array( 'post_type' => $type, 'post__in' => $unique_id, 'paged' => $paged, 'posts_per_page' => 5, 'orderby' => 'meta_value', 'meta_key' => 'Stato', 'order' => 'ASC' );
And maybe you need this plugin to have the posts in the correct order: http://wordpress.org/extend/plugins/sort-query-by-post-in/
Thanks keesiemeijer, i’ll try it out 😉
I tested this and you need the (very small) plugin:
Activate the plugin and test it with this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args=array( 'post__in' => $unique_id, 'paged' => $paged, 'posts_per_page' => 5, 'orderby' => 'post__in', 'order' => 'ASC' );
So i did all, but it seems that nothing is working.
So i paste here my code http://pastebin.com/aBhHsT8T
and the site http://www.m2square.eu/immobili-pagina
I will test it with the meta “value” added.
I will test it with the meta “value” added.
Thanks 😉
EDIT:
I activated that plugin also.
So i did all, but it seems that nothing is working.
Are no posts returned?
If I test it with this it works:
$args = array( 'meta_query' => array( 'relation' => 'OR', array('key' => 'state', 'value' => 'unsold'), array('key' => 'state', 'value' => 'booked' )) ); $firstposts = get_posts($args); $args = array( 'meta_key' => 'state', 'meta_value' => 'sold' ); $lastposts = get_posts($args); // rest of the code
I haven’t used all your code and have not tested it on a custom post type though.
Are all of the three “state” values (as in my example) used in posts?
Of course they are!
But the problem is not there. $firstposts is not empty and $lastposts too. Even $mergedposts and $unique_id are not empty. But when i do
$args=array( 'post__in' => $unique_id, 'paged' => $paged, 'posts_per_page' => 5, 'orderby' => 'post__in', 'order' => 'ASC' ); query_posts( $args );
i have no results 🙁
That’s strange. It just works on my test site. Try with this:
$args=array( 'post__in' => $unique_id ); query_posts( $args );
to see if it pulls all the correct posts. Try it also with all plugins deactivated.
- The topic ‘How to create a query that set some elements at the end of the returned array’ is closed to new replies.