我有一个页面,显示一个帖子类型列表,并希望在此页面上添加搜索/过滤功能。 但是,我对ajax请求有库存,并尝试在标题上使用PostQuery。看起来PostQuery不适用于帖子标题。因此,我首先考虑进行标准的WP查询,以使我的帖子按标题过滤;然后获取此查询返回的所有帖子ID,并将其推入PostQuery,以获取适合我的视图的结构。
所以我想知道是否有更好的解决方案:/
这是我现在正在做的事情:
在我的function.php中,我像这样设置路线:
2
my-post-type.php:
id
使用搜索表单(list.twig)的我的树枝视图:
3
这是我的list-item.twig,其中显示带有分页的列表:
Routes::map('/my-post-type',function ($params) {
if (!is_user_logged_in()) {
wp_redirect(home_url().'/login');
exit();
}
$user = wp_get_current_user();
if ($user->roles[0]=="subscriber") {
$post_type="publish";
} else {
$post_type="any";
}
$query = 'post_type=my-post-type&order=ASC&orderby=title&post_status='.$post_type;
Routes::load('my-post-type.php',$params,$query,200);
});
我在JS端的ajax请求:
<?php
use Timber\Timber;
use Timber\Post;
$context = Timber::context();
$context['post']= new Post();
$url = $_SERVER['REQUEST_URI'];
if (strpos($url,'/page/')==true){
$url = substr($url,strpos($url,"page/"));
}
$context['url'] = $url;
\Timber\Timber::render('my-post-type/list.twig',$context);
最后,PHP端的Ajax请求:
<div class="list-search">
<form id="search" method="GET" action="{{ site.link }}/wp-admin/admin-ajax.php" class="procedure">
<input type="text" class="search-field" name="searchName" placeholder="Post name">
<input type="hidden" name="action" value="dynamic_search">
<input type="hidden" name="entity" value="my-post-type">
<input type="submit" value="Search">
</form>
</div>
<div>
{% include 'my-post-type/list-item.twig' %}
</div>
但是,通过这种解决方案,我可以对标题的特定部分进行过滤,但是分页不起作用。我不知道如何使其与我的ajax返回一起使用。 现在的结果是:我的分页刷新了页面,而我丢失了Ajax过滤。
感谢您的帮助和时间。