Wordpress order posts by post type

后端 未结 5 950
北海茫月
北海茫月 2020-12-17 04:30

I am trying to create a system which fetches post from 2 post types and i want to display the posts order by their post type. I am trying to first show the posts from one po

相关标签:
5条回答
  • 2020-12-17 04:39

    Since Wordpress 4.0 you can just pass the option type (post_type) to the orderby parameter in the WP_Query object.

    orderby (string | array) - Sort retrieved posts by parameter. To order by post type just pass:

    • 'type' - Order by post type (available since Version 4.0). ('post_type' is also accepted.)
    0 讨论(0)
  • 2020-12-17 04:40

    As seen in the docs https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters , you can use the regular wp query by passing an array to 'orderby' :

    $wp_query = new WP_Query(
      array(
          's' => $search,
          'orderby'=> array('type'=>'DESC', 'title'=>'ASC'),
          'paged'=>$paged
      )
    );
    if ( $wp_query->have_posts() ) {
        while ( $wp_query->have_posts() ) {
            $wp_query->the_post();
            // do your loop stufff here
        }
    }
    
    0 讨论(0)
  • 2020-12-17 04:42

    Finally i got the solution and it can be done using a filter.

    The solution looks like this.

    add_filter( 'posts_request' , 'modify_request' );
    function modify_request( $query) {
        global $wpdb;
        if(strstr($query,"post_type IN ('review', 'directory')")){
            $where = str_replace("ORDER BY {$wpdb->posts}.post_date","ORDER BY {$wpdb->posts}.post_type",$query);
        }
        return $where;
    }
    
    0 讨论(0)
  • 2020-12-17 04:42

    Order per post types with custom post type order:

    add_filter('pre_get_posts', function ($query) {
    
        if ($query->is_search && !is_admin()) :
            $query->set('post_type', [ 'cpt-1', 'cpt-2', 'post', 'cpt-3']);
        endif;
    
        return $query;
    });
    
    
    
    add_filter('posts_orderby', function ( $order ) {
    
        if ( ! is_admin() ) :
    
            if ( is_search() && is_main_query() ) :
    
                global $wpdb;
                $order = "FIELD( post_type, 'cpt-1', 'cpt-2', 'post' ), {$wpdb->posts}.post_modified DESC";      
            endif;
    
        // Disable this filter for future queries!
        remove_filter( current_filter(), __FUNCTION__);
        endif;
    
        return $order;
    });
    
    0 讨论(0)
  • 2020-12-17 04:45

    First solution which comes to my mind is to get posts only for first category. Reset query (wp_reset_query()) and get them again for second category. You need to double up your code of course, but I don't see any other solution - it's WordPress - you're very limited.

    Second solution is to query database directly like this:

    $result = mysql_query('SELECT * 
    FROM  `wp_posts` 
    WHERE post_type =  "page"
    OR post_type =  "post"
    ORDER BY post_type ASC , post_date DESC ');
    
    $posts = array();
    
    if($result) {
        while ($row = mysql_fetch_assoc($result)) {
            $posts[] = $row;
        }
    }
    
    mysql_free_result($result);
    
    echo('<pre>');
    var_dump($posts);
    echo('</pre>');
    die();
    

    Notice that I've used post and page types as these are default, and exist in my WP installation. But you can use different. You can play with this query as much as you want.

    0 讨论(0)
提交回复
热议问题