Wordpress order posts by post type

后端 未结 5 970
北海茫月
北海茫月 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: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('
    ');
    var_dump($posts);
    echo('
    '); 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.

提交回复
热议问题