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

wp query - WP_Query() not filtering posts for category

programmeradmin0浏览0评论

I'm trying to create a custom page for a WordPress theme, which is a child theme of the Twenty Twenty theme. I'm simply trying to use WP_Query() to filter out posts so that it only displays posts with the category slug locations, however it produces two issues.

1.) It does not filter out the posts, and shows posts in any category.

2.) The error Notice:

Undefined offset: 9 in /Users/g/Documents/MAMP/childtheme/wp-includes/class-wp-query.php on line 3271 repeats over and over with increasing offsets numbers until the browser crashes.

$q = new WP_Query( array('category_name' => 'locations' ));

if ( $q->have_posts() ) {

    while ( $q->have_posts() ) {

        the_post();
        get_template_part( 'template-parts/content', get_post_type() );

    }
} 

I have a feeling it's something silly, but I have been at it for a few hours and haven't found the issue. Any help would be appreciated.

I'm trying to create a custom page for a WordPress theme, which is a child theme of the Twenty Twenty theme. I'm simply trying to use WP_Query() to filter out posts so that it only displays posts with the category slug locations, however it produces two issues.

1.) It does not filter out the posts, and shows posts in any category.

2.) The error Notice:

Undefined offset: 9 in /Users/g/Documents/MAMP/childtheme/wp-includes/class-wp-query.php on line 3271 repeats over and over with increasing offsets numbers until the browser crashes.

$q = new WP_Query( array('category_name' => 'locations' ));

if ( $q->have_posts() ) {

    while ( $q->have_posts() ) {

        the_post();
        get_template_part( 'template-parts/content', get_post_type() );

    }
} 

I have a feeling it's something silly, but I have been at it for a few hours and haven't found the issue. Any help would be appreciated.

Share Improve this question edited Jun 10, 2020 at 6:07 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Jun 9, 2020 at 19:21 MeiMei 32 bronze badges 1
  • You need only the function the_post(); to map to your custom loop $q, like $q->the_post(); and the filter should run. – bueltge Commented Jun 10, 2020 at 6:08
Add a comment  | 

1 Answer 1

Reset to default 0

the_post() operates on the main query, but this isn't the main query you're looping over, you need to use $q->the_post()

Remember:

  • the_post() is the same as global $wp_query; $wp_query->the_post();
  • All post loops are WP_Query post loops if you dig down deep enough, even get_posts has a WP_Query inside it
  • There can only be 1 current post in $post, calling the_post sets that global variable.

This is what a standard post loop should look like:

$args = [
    // parameters go here
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) { 
    while ( $query->have_posts() ) {
        $query->the_post();
        // display the post
        the_title();
        the_content();
    }
    wp_reset_postdata();
} else {
    esc_html_e( 'no posts were found', 'textdomain' );
}
发布评论

评论列表(0)

  1. 暂无评论