Skip to main content
added 4 characters in body
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

YourThe filter has a bug in it, namely when you call is_main_queryit calls is_main_query, you'reit's not checking if the passed query$query is the main query, yourit's checking if the currently active querycurrently active query is the main query, which will always be true.

So instead try this:

add_action( 'pre_get_posts', 'modify_frontpage_main_query' );
function modify_frontpage_main_query( \WP_Query $query ) : void {
    if ( $query->is_front_page() && $query->is_main_query() ) {
        $query->set( 'post_type', 'some_post_type' );
    }
}

Your filter has a bug in it, namely when you call is_main_query, you're not checking if the passed query is the main query, your checking if the currently active query is the main query, which will always be true.

So instead try this:

add_action( 'pre_get_posts', 'modify_frontpage_main_query' );
function modify_frontpage_main_query( \WP_Query $query ) : void {
    if ( $query->is_front_page() && $query->is_main_query() ) {
        $query->set( 'post_type', 'some_post_type' );
    }
}

The filter has a bug in it, namely when it calls is_main_query, it's not checking if the passed $query is the main query, it's checking if the currently active query is the main query, which will always be true.

So instead try this:

add_action( 'pre_get_posts', 'modify_frontpage_main_query' );
function modify_frontpage_main_query( \WP_Query $query ) : void {
    if ( $query->is_front_page() && $query->is_main_query() ) {
        $query->set( 'post_type', 'some_post_type' );
    }
}
add typehints and use newer code block guards for syntax highlighting
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

Your filter has a bug in it, namely when you call is_main_query, you're not checking if the passed query is the main query, your checking if the currently active query is the main query, which will always be true.

So instead try this:

add_action( 'pre_get_posts', 'some_name');
function some_name($query) {
    if ($query->is_front_page() && $query->is_main_query()) {
        $query->set( 'post_type', 'some_post_type' );
        return;
    }
}
add_action( 'pre_get_posts', 'modify_frontpage_main_query' );
function modify_frontpage_main_query( \WP_Query $query ) : void {
    if ( $query->is_front_page() && $query->is_main_query() ) {
        $query->set( 'post_type', 'some_post_type' );
    }
}

Your filter has a bug in it, namely when you call is_main_query, you're not checking if the passed query is the main query, your checking if the currently active query is the main query, which will always be true.

So instead try this:

add_action( 'pre_get_posts', 'some_name');
function some_name($query) {
    if ($query->is_front_page() && $query->is_main_query()) {
        $query->set( 'post_type', 'some_post_type' );
        return;
    }
}

Your filter has a bug in it, namely when you call is_main_query, you're not checking if the passed query is the main query, your checking if the currently active query is the main query, which will always be true.

So instead try this:

add_action( 'pre_get_posts', 'modify_frontpage_main_query' );
function modify_frontpage_main_query( \WP_Query $query ) : void {
    if ( $query->is_front_page() && $query->is_main_query() ) {
        $query->set( 'post_type', 'some_post_type' );
    }
}
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

Your filter has a bug in it, namely when you call is_main_query, you're not checking if the passed query is the main query, your checking if the currently active query is the main query, which will always be true.

So instead try this:

add_action( 'pre_get_posts', 'some_name');
function some_name($query) {
    if ($query->is_front_page() && $query->is_main_query()) {
        $query->set( 'post_type', 'some_post_type' );
        return;
    }
}