如何按包含自定义字段的列对自定义帖子表进行排序

时间:2016-10-06 作者:Steve

我有一个自定义的帖子类型,叫做laptop 使用自定义字段,如CPU, OS, RAM 使用Advanced Custom Fields 插件。

我正在显示一个笔记本电脑表格,其中表格标题是一行自定义字段,下面的每一行是笔记本电脑名称,以及每个相应列单元格中的自定义字段值。

我希望能够通过单击表格对表格进行排序TH 包含自定义字段名的单元格。

有人指着我meta query clauses 通过ACF的支持,但我对这些已经力不从心了。

我非常感谢您在生成URL时提供一些帮助,以围绕每个TH 要素

2 个回复
最合适的回答,由SO网友:Paul \'Sparrow Hawk\' Biron 整理而成

要跟进@rock3t的评论,您真的需要在用户单击列标题时查询后端吗?

您是否考虑过使用jQuerytablesorter? 它允许最终用户通过DOM操作对HTML表进行排序。

我从未在WP项目中使用过它,但我在我构建的其他项目中广泛使用过它,而且效果很好。它是very 可配置,因此我相信它将满足您的需求。

tablesorter是not WP-Core中包含的jQuery插件之一。因此,您必须下载它(从上面的链接),将其包含在您的插件/主题中的某个位置,并将其排队。然后,将一个简单的JS文件排入队列,该文件如下所示:

(function ($) {
    $(document).ready (function () {         
        $(\'#id_of_your_table\').tablesorter ({widgets: [\'zebra\']}) ;     
        }) ;
})(jQuery) ;
您可以阅读更多关于如何在docs, 包括各种其他参数tablesorter() 方法

SO网友:prosti

此参考很简单:https://codex.wordpress.org/Class_Reference/WP_Query

orderby应为meta_value

“meta\\u value”-请注意,“meta\\u key=keyname”也必须存在于查询中。

您的密钥名称应该是以下名称之一:CPU、OS、RAM。。。

您还可以考虑WP_Meta_Queryhttps://codex.wordpress.org/Class_Reference/WP_Meta_Query

更新我得到了这个结果

    Array
(
    [0] => WP_Post Object
        (
            [ID] => 3233
            [post_author] => 1
            [post_date] => 2016-11-19 12:51:43
            [post_date_gmt] => 2016-11-19 12:51:43
            [post_content] => abc
            [post_title] => Mac Air Pro 9G
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => mac-air-pro-3
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-11-19 12:52:19
            [post_modified_gmt] => 2016-11-19 12:52:19
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://steve.com/laptop/mac-air-pro-1-copy/
            [menu_order] => 0
            [post_type] => laptop
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [1] => WP_Post Object
        (
            [ID] => 3232
            [post_author] => 1
            [post_date] => 2016-11-19 12:46:50
            [post_date_gmt] => 2016-11-19 12:46:50
            [post_content] => abc
            [post_title] => Mac Air Pro 8G
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => mac-air-pro-2
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-11-19 12:52:47
            [post_modified_gmt] => 2016-11-19 12:52:47
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://steve.com/laptop/mac-air-pro-1-copy/
            [menu_order] => 0
            [post_type] => laptop
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [2] => WP_Post Object
        (
            [ID] => 3231
            [post_author] => 1
            [post_date] => 2016-11-19 12:12:38
            [post_date_gmt] => 2016-11-19 12:12:38
            [post_content] => abc
            [post_title] => Mac Air Pro 4G
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => mac-air-pro-1
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-11-19 12:52:57
            [post_modified_gmt] => 2016-11-19 12:52:57
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://steve.com/?post_type=laptop&p=3231
            [menu_order] => 0
            [post_type] => laptop
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)
这是单台笔记本电脑的内容。php文件:

<?php
/**
 * The template for displaying all single posts.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package microformats
 */

get_header(); 


// WP_Query arguments
$args = array (
    \'post_type\'              => array( \'laptop\' ),
    \'post_status\'            => array( \'published\' ),
     //\'s\'                      => \'atom\', // only if you need that
    \'nopaging\'               => true,
    \'posts_per_page\'         => \'-1\',
    \'ignore_sticky_posts\'    => true,
    // order
    \'orderby\'                => \'meta_value\', 
    \'meta_key\'               => \'ram\',
    \'order\'                  =>\'DESC\',
    /* you will need this only if
    \'meta_query\'             => array(
        array(
            \'key\'       => \'ram\', // or os, or cpu
            \'value\'     => \'4\', // meaning 4G of ram
            \'compare\'   => \'>\',  // meaning only biggar
            \'type\'      => \'NUMERIC\',
        ),
    ),
    */
);


// The Query
$query = new WP_Query( $args );
print_r($query->posts);


?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

        <?php while ( have_posts() ) : the_post(); ?>

            <?php get_template_part( \'template-parts/content\', \'single\' ); ?>

            <?php
                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) :
                    comments_template();
                endif;
            ?>


            <hr class="fat" />              

        <?php endwhile; // End of the loop. ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: