【问题标题】:Wordpress orderby title using menu_order insteadWordpress orderby 标题使用 menu_order 代替
【发布时间】:2017-09-20 19:05:14
【问题描述】:

我想检索自定义帖子并希望按标题对其进行排序。但是,当我转储发送的确切 SQL 请求时,我发现 orderby 使用的是 menu_order 而不是 title

代码如下:

$args=array(
  'post_type' => 'custom_post',
  'orderby' => 'title',
  'order' => 'ASC',
  'post_status' => 'publish',
  'posts_per_page' => '-1',
);

这里是转储

"SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'custom_post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.menu_order ASC "

因此,当我检索自定义帖子时,它不是我想要的顺序。 感谢您的帮助

【问题讨论】:

  • 你用什么方法查询帖子?

标签: sql wordpress


【解决方案1】:

您可以创建新的 WP_Query 实例以实现确切的预期结果。

<?php
$args = array(
        'post_type' => 'custom_post_type',
        'order'     => 'ASC',
        'orderby'   => 'title',
    );
$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <div class="smal-sec">
        <h3><?php the_title();?></h3>
        <?php the_content();?>
    </div>
<?php
endwhile;
endif;
wp_reset_query();  // Restore global post data stomped by the_post().
?>

此查询将帮助您按标题顺序显示所有帖子 (A -&gt; Z)。请确保没有这样的插件,这实际上覆盖了WP_Query 实例。

【讨论】:

    猜你喜欢
    • 2012-08-16
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2016-06-22
    • 2014-11-18
    相关资源
    最近更新 更多