自定义帖子类型管理列表上未显示分页链接

时间:2019-05-13 作者:Gogo

In my own plugin I have a custom post type faq. In the admin listing are 14 faq posts. In the Wordpress Settings under Reading is 10 posts per pages set. Additionally, I have added a pre_get_posts function with the following content.

function hep_pre_get_posts_faq(WP_Query $query)
{
    // check if current query is not the main query
    if (!$query->is_main_query()) {
        return;
    }

    // edit the query only when post type is \'faq\'
    if (!is_admin() || $query->query[\'post_type\'] == \'hep_faq\') {
        return;
    }

    // Protect against arbitrary paged values
    $paged = (get_query_var(\'paged\')) ? absint(get_query_var(\'paged\')) : 1;

    // set pagination
    $query->set(\'posts_per_page\', 10);
    $query->set(\'paged\', $paged);
}

The list is displayed correctly with 10 entries. Where the pagination links are, there is written 14 items without pagination links. And this for a good reason, because if I force the display you see 1 of 1. But here should be 1 of 2 because there are 14 posts.

This already happened to me with another cpt. The strange thing here is that everything works fine on the live-system on the server. Only on the local development-system it doesn\'t display the pagination links because of the same reason as described above.

Of course this confuses me a lot. As it seems not to be the code, because it\'s exactly the same on the development system as on the live system.

I would be very happy for any help concerning this. The plugin is already live and is used productively by my customers.

Here are all query parameters:

Request
/wp-admin/edit.php
?post_type=hep_faq

Query String
post_type=hep_faq
&posts_per_page=20

Query Vars
cache_results   1
comments_per_page   50
lazy_load_term_meta 1
order   DESC
paged   1
post_type   hep_faq
posts_per_page  10
update_post_meta_cache  1
update_post_term_cache  1

and the custom post type arguments:

$args = array(
    \'label\'               => \'faq\',
    \'description\'         => \'FAQ\',
    \'labels\'              => $labels,
    \'supports\'            => array(\'title\'),
    \'hierarchical\'        => false,
    \'capabilities\'        => $caps,
    \'public\'              => false,
    \'show_ui\'             => true,
    \'show_in_nav_menus\'   => false,
    \'show_in_menu\'        => \'hep-dashboard\',   
    \'show_in_admin_bar\'   => true,
    \'menu_position\'       => 1,
    \'menu_icon\'           => plugins_url() . "/img/contact.png",
    \'can_export\'          => true,
    \'has_archive\'         => true,
    \'exclude_from_search\' => true,
    \'publicly_queryable\'  => true,
    \'capability_type\'     => array(\'hep_faq\', \'hep_faqs\'),
    \'map_meta_cap\'        => true,
    \'query_var\'           => true,
    \'rewrite\'             => array(\'slug\' => get_option(\'hep_faq_slug\')),
);
1 个回复
最合适的回答,由SO网友:nmr 整理而成

每页帖子数”(Posts per pages)此处不适用于“Settings”(设置)中的限制(面板中的帖子列表),列表的默认大小为20.

您的分页无法正常工作,因为您使用pre_get_posts 用于更改per_page 限制,这不是正确的方法。这个per_page 限制被覆盖pre_get_posts 筛选器仅在某些情况下有效(例如,对于层次结构类型,它不起作用),并且它会影响从DB获取的帖子数量,但会影响分页原始的帖子数量per_page 将使用值。

仪表板中每页的帖子数要更改仪表板中列表每页的帖子数,请使用挂钩:

add_filter(\'edit_hep_faq_per_page\', \'se337791_hepfaq_posts_per_page\');
function se337791_hepfaq_posts_per_page( $posts_per_page )
{
    return 10;
}

// -- OR --
add_filter(\'edit_posts_per_page\', \'se337791_posts_per_page\', 20, 2);
function se337791_hepfaq_posts_per_page( $posts_per_page, $post_type )
{
    if ( \'hep_faq\' == $post_type )
        return 10;
    return $posts_per_page;
}

相关推荐

在Get_the_Posts_Pagination函数中编辑分页文本

我想在链接模板中编辑screen\\u reader\\u文本。php我可以在一个主题中这样做,这样它就不会在更新时被覆盖。看起来过滤器是最好的选择,但我找不到关于使用什么过滤器的文档。这是我想从链接模板更改的代码。php: if ( $GLOBALS[\'wp_query\']->max_num_pages > 1 ) { $args = wp_parse_args( $args, array( \'mid_size\' =&