WordPress.org

Ready to get started?Download WordPress

Forums

sorting posts by post type or any other variable.. (2 posts)

  1. black_lady
    Member
    Posted 1 year ago #

    I have 3 custom post type (a, b, c) and one custom taxonomy. I want display (in this taxonomy archive page) my posts by order: first posts "a", next posts "b" and then posts "c". Now (default) my custom posts (a, b and c) are order by date.
    Maybe it is possible ordering posts by CPT slug or by any other variable (custom post field)?
    Maybe is it plugin where i can tape order number next to CPT name f.e.:
    CPT order
    post -2-
    book -1-
    my_another_custom_post_type -3-

    If you know some plugin..
    I am searching since 2 week, and I'am losing my hope..
    Please, help :)
    sorry for my english..

  2. vtxyzzy
    Member
    Posted 1 year ago #

    You should be able to do this using a filter. The exact code depends on the way your query is written. If you post a few lines from your template, including the query, I will try to show you the exact code.

    In general, you would add the following just before the query:

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');
    
    $mam_global_orderby = " $wpdb->posts.post_type ASC, $wpdb->posts.post_date DESC ";

    and add this line after the query to clear the filter:

    $mam_global_orderby = '';

Topic Closed

This topic has been closed to new replies.

About this Topic