【问题标题】:Wordpress: Custom Post Types filter, sort and searchWordpress:自定义帖子类型过滤、排序和搜索
【发布时间】:2012-12-12 22:12:47
【问题描述】:

我正在使用自定义帖子类型在页面上显示一些信息。

Wordpress Custom Post type displaying list of custom post type on existing page

有没有办法为数据添加过滤、排序和搜索功能?

我已经设法让分页工作“domain.com/page/2”

 $the_query = new WP_Query(array('post_type' => 'custom_post_type', 'posts_per_page' => 25, 'paged' => $paged, 'orderby'=>'title', 'order' => 'ASC'));

我使用 jquery DataTable (http://datatables.net/) 添加排序、分页和搜索,但是当您在 1000 多条记录上使用 Datatable 时,它​​变得非常缓慢,有时它会显示有关内存不足的 php 错误。 这始终是一个临时解决方案,直到我想出一种使用 Wordpress 正确完成它的方法

【问题讨论】:

    标签: wordpress filter custom-post-type


    【解决方案1】:

    您提到了一个用于对数据进行排序的 jQuery 工具,但这对于来自数据库的数据来说不是一个好主意。 jQuery 排序工具旨在使表格更具可读性,并帮助用户处理已经显示在页面上的数据。使用数据库,想法更多的是只获取所需的数据,而不是获取页面中的所有内容,然后对元素进行排序或过滤以将它们从视图中删除(但仍在页面中,这意味着它更重,下载时间更长,更难处理等)。 您已经设法为您的自定义帖子类型实现分页,这很好。现在只需包含一个搜索表单。默认的 WordPress 搜索表单 (searchform.php) 如下所示:

                <form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
                    <div><label class="screen-reader-text" for="s">Search for:</label>
                        <input type="text" value="" name="s" id="s" />
                        <input type="submit" id="searchsubmit" value="Search" />
                    </div>
                </form>
    

    您只需在其中添加一小行即可将搜索限制为您的自定义帖子类型:

                        <input type="hidden" name="post_type" value="name_of_your_post_type" />
    

    所以它会是这样的:

                <form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
                    <div><label class="screen-reader-text" for="s">Search for:</label>
                        <input type="text" value="" name="s" id="s" />
                        <input type="submit" id="searchsubmit" value="Search" />
                        <input type="hidden" name="post_type" value="name_of_your_post_type" />
                    </div>
                </form>
    

    【讨论】:

      【解决方案2】:

      感谢 barakadam,我还没有尝试搜索,但我能够找出 order 和 orderby

      我使用 $_GET 来获取 order 和 orderby 的值,并在

      中传递了这些变量

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-17
        • 2015-09-08
        • 1970-01-01
        • 2023-03-29
        • 2014-05-20
        • 1970-01-01
        • 1970-01-01
        • 2012-09-14
        相关资源
        最近更新 更多