Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit 404 pages from front-end search results #15

Open
joemaller opened this issue Aug 27, 2021 · 1 comment
Open

Omit 404 pages from front-end search results #15

joemaller opened this issue Aug 27, 2021 · 1 comment
Assignees

Comments

@joemaller
Copy link
Member

Pages assigned the 404 template should not be appearing in front-end search results.

@joemaller joemaller self-assigned this Aug 27, 2021
@joemaller
Copy link
Member Author

Something like this:

function omit404s($query)
{
    if ($query->is_search && !is_admin()) {
        $args = [
            "posts_per_page" => -1,
            "post_type" => "page",
            "meta_query" => [
                [
                    "key" => "_wp_page_template",
                    "value" => "404.php",
                ],
            ],
        ];
        $templateQuery = new \WP_Query($args);
        $omitIDs = wp_list_pluck($templateQuery->posts, "ID");
        $query->set("post__not_in", $omitIDs);
    }
    return $query;
}
add_filter("pre_get_posts", "omit404s");

Does the name of a plugin-injected template match the file in the plugin or is that something we get to set? ref: #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant