• Resolved Jesse Friedman

    (@professor44)


    Let me start by thanking anyone out there willing to help with this problem.

    I am in development of custom plugin and everything’s going well.

    The plugin does it’s job and comes up with a list of posts to display. This list is an array called $provIDlist and it contains a list of post id numbers.

    I then use query_posts to display the posts as you can see in the below code. This works beautifully except for the order of things.

    $args = array(
    'posts_per_page' => 25,
    'post__in' => $provIDlist,
    'orderby' => none
    );
    echo query_posts($args);

    What I want is the posts to display in the order they exist in the array. Right now setting the ‘orderby’ to ‘none’ it actually displays them in ascending order of post_id. So “none” isn’t really no order.

    Is there anyway to have the posts display in the order they are queried from the array?

Viewing 4 replies - 1 through 4 (of 4 total)
  • With 3.0 there might be a better way but for now this is an example of the only way I could do that…

    <?php
      function filter_orderby($orderby = '') {
      $orderby = " FIELD(ID, 350, 194, 306, 1, 421)";
       return $orderby;
      }
    add_filter('posts_orderby', 'filter_orderby');
        $args=array(
          'post__in' => array(350,194,306,1,421),
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '5 ORDERED Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p> <?php the_category(', '); ?> -
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter Jesse Friedman

    (@professor44)

    @michaelh

    Thank you for the reply, I’ve taken what you shared and modified it a bit. In the section below how can I replace the list of posts with an array?

    $orderby = 'FIELD(ID, 21748, 18589, 18591, 18613, 18641, 18661, 18683, 18777, 18807, 18826, 18868, 18907, 18934, 19038, 19045, 19117, 19129, 19136, 18577)';

    I would like it to look like this:

    $orderby = 'FIELD(ID, $provIDlist)';

    I’ve tried it a million ways and can’t get it to work the way I want. However if I write out all the posts the way you have it, it works beautifully.

    thanks, again

    Thread Starter Jesse Friedman

    (@professor44)

    I fixed it:

    function filter_orderby($orderby = '') {
    global $provIDlist;
    foreach ($provIDlist as $id_item)$orderitby .= ', ' . $id_item;
    $orderby = 'FIELD(ID' . $orderitby . ')';
    
    //echo $orderby;
    return $orderby;
    }

    I had to run the $provIDlist array through a foreach to parse out each id

    Let me know if you have any ideas on how to better perform that task.

    thanks

    this?

    $provIDlist = implode(',',$provIDlist);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Creating Custom Plugin: Need help with query_posts’ is closed to new replies.