【问题标题】:wordpress sort by custom fieldwordpress 按自定义字段排序
【发布时间】:2013-07-10 01:06:20
【问题描述】:

我希望有人可以帮助我解决这个问题。我一直在阅读其他帖子、Codex 并尝试其他代码,但无法修复我的查询。

我正在创建一个包含作者列表的页面。作者分为两类,我需要按姓氏、名字排序。我想通过自定义字段 wpcf-sortname(来自 Types 插件)对它们进行排序。

我从查询中得到了正确的结果,但结果是按 ID 排序的。

注意:我不太擅长查询,但我们将不胜感激。

我试过了:

<?php query_posts(array('category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>meta_value,'order'=>ASC,));

if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>" class="participants"><?php the_title(); ?></a><br />
<?php endwhile; // end of the loop. ?>

还有:

<?php
$args = array( 'category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>wpcf-sortname,'order'=>ASC,'posts_per_page'=>-1);
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post); ?> 
<div>
        <a href="<?php the_permalink() ?>" class="participants" style="color:red"><?php the_title(); ?></a><br /> 
</div>

【问题讨论】:

    标签: wordpress custom-fields


    【解决方案1】:

    试试这个

    <?php query_posts(array('category__and'=>array(48,49),'meta_key'=>'wpcf-sortname','orderby'=>'meta_value','order'=>ASC,));if ( have_posts() ) while ( have_posts() ) : the_post(); ?><a href="<?php the_permalink() ?>" class="participants"><?php the_title(); ?></a><br /><?php endwhile; // end of the loop. ?>
    

    我刚刚在 metakey 和 orderby 字段中加上了单引号。我认为这对你有用。

    【讨论】:

    • 我所有的数据现在都按正确的顺序排序了!但是,我注意到一个奇怪的问题,由于某种原因,其中一个作者的名字没有出现。它只出现在一个类别中。知道为什么吗? &lt;?php query_posts(array('category__and'=&gt;array(48,37),'meta_key'=&gt;'wpcf-sortname','orderby'=&gt;'meta_value','order'=&gt;ASC,));if ( have_posts() ) while ( have_posts() ) : the_post(); ?&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" class="participants"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;br /&gt;&lt;?php endwhile; // end of the loop. ?&gt;
    • 刚刚弄清楚原因,看起来如果我没有填写自定义字段,帖子就不会显示。
    猜你喜欢
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多