WordPress.org

Ready to get started?Download WordPress

Forums

How to create a query that set some elements at the end of the returned array (17 posts)

  1. Iperdesign_IT
    Member
    Posted 2 years ago #

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

    And 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) red

    What should i do? Thanks for all!

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    ?
    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?

  3. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with two (multiple) loops: http://codex.wordpress.org/The_Loop#Multiple_Loops

  4. Iperdesign_IT
    Member
    Posted 2 years ago #

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

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  6. Iperdesign_IT
    Member
    Posted 2 years ago #

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

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    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'
    );
  8. keesiemeijer
    moderator
    Posted 2 years ago #

    And maybe you need this plugin to have the posts in the correct order: http://wordpress.org/extend/plugins/sort-query-by-post-in/

  9. Iperdesign_IT
    Member
    Posted 2 years ago #

    Thanks keesiemeijer, i'll try it out ;)

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    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'
    );
  11. Iperdesign_IT
    Member
    Posted 2 years ago #

    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

  12. keesiemeijer
    moderator
    Posted 2 years ago #

    I will test it with the meta "value" added.

  13. Iperdesign_IT
    Member
    Posted 2 years ago #

    I will test it with the meta "value" added.

    Thanks ;)

    EDIT:

    I activated that plugin also.

  14. keesiemeijer
    moderator
    Posted 2 years ago #

    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?

  15. Iperdesign_IT
    Member
    Posted 2 years ago #

    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 :(

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  17. Iperdesign_IT
    Member
    Posted 2 years ago #

    Nothing happen :(

    Well never mind, i'll use a workaround:

    $args=array(
    	'post_type' => $type,
    	'paged' => $paged,
    	'posts_per_page' => 5,
    	'orderby'	=>	'meta_value',
    	'meta_key'	=>	'Stato',
    	'order'		=>	'ASC'
    );

    So all 'sold' and 'booked' are in the last positions ( and my customer has no problem with this solution )

    By the way i'll try on holidays to solve this problem ;)

    Thanks for all keesiemeijer ;)

Topic Closed

This topic has been closed to new replies.

About this Topic