Skip to content

pre_get_posts: targeting the right query #3

@cheh

Description

@cheh

I want you to pay attention that pre_get_posts action in your plugin has an effect on all queries:

add_action('pre_get_posts', 'commentnavi_allcomments');
function commentnavi_allcomments() {
	if (intval(get_query_var('comment-all')) == 1) {
		set_query_var('comments_per_page', 9999);
	}
}

And it throws some unexpected behavior and/or conflicts with other plugins, e.g. https://wordpress.org/support/topic/backend-error-since-version-1-6-4-1/

I'm pretty sure that you should add a couple of conditional tags to target the right query:

add_action('pre_get_posts', 'commentnavi_allcomments');
function commentnavi_allcomments() {
	if ( ! is_admin() && is_main_query() && intval(get_query_var('comment-all')) == 1) {
		set_query_var('comments_per_page', 9999);
	}
}

More information here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions