WordPress.org

Ready to get started?Download WordPress

Forums

Sorting posts from a custom input - how to remove default sorting (5 posts)

  1. ellvix
    Member
    Posted 1 year ago #

    I'm trying to do a post sort based on a premade list I have, and am wondering how to disable the default wordpress sorting (and if I'm doing this right at all).

    I'm displaying heaps of posts on the main page. I fetch post ids from a secondary table with the $wpdb var (with ORDER BY specified there). I'd like to be able to tell wordpress to display my fetched id array exactly as is - display only the things in that array, and display them in that order. I'm trying to patch this without changing too much, as I already have a few plugins (by which I mean that a direct query / main loop modification is out), so I'm using a filter for pre_get_posts.

    From what I've seen, I can tell wordpress to display my id array with:
    $query->query_vars['post__in'] = $myPostIds ;

    That's good. However, I assume that the order of the myPostIds array will get trashed. Would turning the sorting off do the trick, and is that even the right idea? I could do something like:
    $query->query_vars['orderby'] = "" ; // or null
    (not sure if that works, that's just a guess)

    I would like to know how to get this to sort properly. If there are methods that would do this, fantastic. Or, if anyone can tell me that I'm totally wrong on this whole method, that would be great too.

    Thanks for the help!

  2. bcworkz
    Member
    Posted 1 year ago #

    You can order posts by values contained in any column provided it can be tied back to a post id somehow. That is how queries work. If you already have an array of IDs in the order you want, you don't need a query at all, just do something like this instead of the usual 'loop':

    foreach ($myPostIds as $id) {
      $post = get_post($id);
      echo "<h2>$post->post_title</h2>";
      echo "<p>$post->post_content</p>";
    }

    You can of course add in the usual stuff contained in the normal loop, date, author, whatever. You probably don't want to use this as your main page template. One way to use this template is set it up as custom page template, then create a new page using it. There are other ways if that doesn't work for you.

  3. ellvix
    Member
    Posted 1 year ago #

    Thanks. That's how I would've done it, but I'm trying to avoid messing with the main loop. Any way to do that? I couldn't think of any, and am starting to think that doing a custom loop is the only way to go. So bothersome though. /whine

  4. bcworkz
    Member
    Posted 1 year ago #

    You could put my sample in parallel with the main loop, which one runs would be controlled by a conditional of some sort. Most types of requests run normally in the main loop, only a particular request runs the ordered array loop instead.

  5. ellvix
    Member
    Posted 1 year ago #

    Perhaps I'll try that.

    I think I could also try meta tags. I could take from my array and insert / update the post meta table and then just use a standard meta sort thing. I'll ponder this and decide which is faster. Thanks for the comments.

Topic Closed

This topic has been closed to new replies.

About this Topic