php - 草稿中自定义帖子类型的木材分页问题
问题描述
解释 :
我在我的自定义帖子类型 archive.php 上创建了一个搜索表单,该表单对于使用 Timber(和 Bedrock)的标准用户而言可以正常工作。
现在,我希望至少具有作者角色的用户以发布或草稿状态显示所有帖子。这个想法是允许作者和其他在网站上工作的用户查看帖子在列表视图中的呈现方式。
然而,草稿帖子并没有像预期的那样被 wordpress 或木材计算在内。我的意思是,我发布了 5 个帖子,其中 1 个在草稿中,我设置了我的 wordpress 设置,以便每页显示 1 个帖子。
作为非作者,我可以毫无问题地从第 1 页转到第 6 页。但是作为作者、编辑或其他更高级别的角色,我应该能够从第 1 页转到第 7 页......并且在第 6 页,由于 Timber 分页,我能够进入“下一页”,但是当我点击它第7页是一个404错误页面...
我期待什么: 我期待能够进入最后一页。
我的代码: 我的 archive-post_type.php 就像:
<?php
use \Timber\Timber;
if (!is_user_logged_in()) {
wp_redirect(home_url().'/login');
exit();
}
global $paged;
if (!isset($paged) || !$paged) {
$paged = 1;
}
$context = Timber::context();
$user = wp_get_current_user();
$isSubscriber=$user->roles[0]=="subscriber"?true:false;
if (isset($_REQUEST["entity"])) {
$search = isset($_REQUEST["searchName"])?$_REQUEST["searchName"]:"";
$searchContain = $_REQUEST["searchType"]==="contain"?true:false;
$args=Search('form', $isSubscriber, $searchContain, $search, $paged);
} else {
$search = "";
$searchContain = true;
$args=Search('form', $isSubscriber, $searchContain, $search, $paged);
}
$context['posts']= new \Timber\PostQuery($args);
\Timber\Timber::render('form/list.twig', $context);
function Search($post_type, $isSubscriber, $searchContain, $search, $paged)
{
global $wpdb;
$query = "
SELECT *
FROM {$wpdb->prefix}posts
WHERE post_type = '" . $post_type . "'"
;
if ($isSubscriber) {
$query .= " AND post_status = 'publish'";
$post_status="publish";
} else {
$post_status="any";
}
if (!empty($search)) {
if ($searchContain) {
$query .= " AND post_title LIKE '%" . $search . "%'";
} else {
$query .= " AND post_title LIKE '" . $search . "%'";
}
}
$results=$wpdb->get_results($query);
if (!empty($results)) {
foreach ($results as $result) {
$posts[] = $result->ID;
}
return $args = [
'post__in' => $posts,
'post_type' => $post_type,
'post_status' => $post_status,
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged
];
}
return [];
}
我的 list.twig :
{% extends 'layout/base.twig' %}
{% block body %}
<nav class="breadcrumb">
{{ breadcrumb() }}
</nav>
<div class="container">
<div class="content-wrap">
<div class="list-title">
<h1>Form list</h1>
</div>
<div class="list-search">
<form id="search" method="GET" action="{{ site.link }}/form" class="form">
<select class="search-type" name="searchType" form="search">
<option selected value="contain">Contain</option>
<option value="start">Start with</option>
</select>
<input type="search" class="search-field" name="searchName" placeholder="Search">
<input type="hidden" name="action" value="dynamic_search">
<input type="hidden" name="entity" value="for">
<input type="submit" value="Search">
</form>
</div>
<div class="list-item">
{% if posts is not empty %}
{% for i, post in posts %}
{% set description = post.meta('form-description') %}
{% set shortcode = post.meta('form-shortcode') %}
{% if shortcode is not empty %}
<div class="list-item__body list-item-form item-{{ i }} {% if post.post_status != "publish" %}not-publish{% endif %}">
{% if post.post_status != "publish" %}
<div class="ribbon ribbon-top-right"><span>Draft</span></div>
{% endif %}
<div class="list-item__title">
<a href="{{ url }}./{{ post.slug }}">
<h2>{{ post.post_title }}</h2>
</a>
</div>
<div class="list-item__description">
{{ description |raw | truncate(15,true, ' ...')}}
</div>
<div class="list-item__more">
<a href="{{ url }}./{{ post.slug }}">Read more <i class="fas fa-arrow-right"></i></a>
</div>
</div>
{% endif %}
{% endfor %}
{% else %}
<p>Forms not found</p>
{% endif %}
</div>
</div>
<div class="list-pagination">
{% if posts.pagination.prev %}
<div class="list-pagination__action list-pagination__prev">
<i class="left fas fa-arrow-left"></i>
<a href="{{posts.pagination.prev.link}}" class="next {{posts.pagination.prev.link|length ? '' : 'invisible'}}">Previous page</a>
</div>
{% endif %}
{% if posts.pagination.next %}
<div class="list-pagination__action list-pagination__next">
<a href="{{posts.pagination.next.link}}" class="next {{posts.pagination.next.link|length ? '' : 'invisible'}}">Next page</a>
<i class="right fas fa-arrow-right"></i>
</div>
{% endif %}
</div>
</div>
{% endblock %}
注意:我遵循了如果我不使用默认查询怎么办? Timber 分页指南的解释部分:https ://timber.github.io/docs/guides/pagination/
解决方案
推荐阅读
- javascript - 在增量中,为什么传递变量的值而不是值和增量?
- angular - 角材料:如何应用
标签上的消息 - windows - Mac 上的 git 将我未触及的文件视为已修改,并且它们也显示为已上演
- javascript - angular js TypeError:无法读取未定义的属性“toLowerCase”
- python - 根据其他列中的值将列中的值更改为 np.nan
- kubernetes - 服务资源中的负载均衡逻辑
- c# - 如何使 ShouldSerialize*() 仅适用于 XML 而不是 JSON 序列化?
- swift - Swift 协议扩展:无法分配给属性:'' 是一个只能获取的属性
- swift - 删除字典中的重复项并仅保留最小值
- ios - TableView 单元格元素上的 AddTarget 函数不起作用(发送到实例的无法识别的选择器)