最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - How do I troubleshoot pre_get_posts with Debug Bar?

programmeradmin1浏览0评论

I am using pre_get_posts() on a custom post type Archive page, to change the sort order. When I view the query using Debug Bar and WP Query Monitor, I can see that my action is indeed firing. If I print_r the $query->query_vars I can see the correct query. But, the post order on the front end doesn't change. I've checked and I'm not using conditionals that aren't set up yet (eg, I'm not using is_post_type_archive() but rather $query->is_post_type_archive()).

There is an awful lot of information presented in Debug Bar and WP Query Monitor - where do I dig in to find out why my posts are being ordered differently (by date) rather than by my specified orderby parameter?

For reference, I am on a sub-site of a WPMU network, using a child theme. My code (in my child theme functions.php) is:

function swm_custom_post_order_sort( $query ) {
    // don't do this on the admin end - we only want this on the front.
    if ( ! is_admin() ) {
        // is it the main query on the Film Archive page?
        if ( $query->is_main_query() && $query->is_post_type_archive( 'film' ) ) {
            $query->set( 'orderby', 'name' );
            $query->set( 'order', 'ASC' );
            $query->set( 'posts_per_page', -1 );
            $query->set( 'suppress_filters', true );
            // print_r( $query );
        }
    }
}
add_action( 'pre_get_posts', 'swm_custom_post_order_sort' );

In fact, none of the $query->set( something ) options work. If I use $query->set( 'posts_per_page', 3 ), I don't get 3 posts, either. Something, somewhere, is overriding this action, maybe?

The custom post type is registered through a plugin (Toolset Types). The settings for the custom post are in this screenshot: .jpg Which maps to the following:

// Register Custom Post Type
function toolset_function() {
    $labels = array( ... lots of labels here );
    $rewrite = array(
        'slug'                  => 'film',
        'with_front'            => true,
        'pages'                 => false,
        'feeds'                 => false,
    );
    $args = array(
        'label'                 => __( 'Film', 'some-text-domain' ),
        'description'           => __( 'Documentary films, including short films.', 'some-text-domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes' ),
        'taxonomies'            => array( 'special-feature', 'availability' ),
        'hierarchical'          => true,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-editor-video',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'rewrite'               => $rewrite,
        'capability_type'       => 'post',
        'show_in_rest'          => true,
    );
    register_post_type( 'film', $args );

}
add_action( 'init', 'toolset_function', 0 );
发布评论

评论列表(0)

  1. 暂无评论