I have the following function to check if the page exists in WordPress by using the path:

    private function page_exists( $path ) {
        $post_types = apply_filters( 'get_page_by_path_post_types', [ 'page', 'post' ] );
        foreach ( $post_types as $pt ) {
            if( get_page_by_path( $path, OBJECT, $pt ) !== null ) {
                return true;
        return false;

The problem is that in multisite the function fails. I searched around but I couldn't find a Multisite function similar to get_page_by_path

  • I'm not sure what you mean by "in multisite the function fails" -- do you mean that the function doesn't work at all, or are you hoping for something that searches every site in your multisite network?
    – Pat J
    Oct 31, 2020 at 3:53
  • Yes, by fails I mean that the path exists, but the function returns false because it doesn't look in the network
    – chifliiiii
    Nov 1, 2020 at 18:33

1 Answer 1


get_page_by_path() will only search in the current site.

If you need to find a page anywhere in your Multisite network, you can do something like this.

function network_page_exists( $path ) {
    $args = array(
         // Max sites to retrieve.
         // If you have a large network, increase this.
        'number' => 100,
    $sites = get_sites( $args );
    $found = false;
    foreach ( $sites as $site ) {
        if ( ! $found ) {
            switch_to_blog( $site->blog_id );
            // Uses your page_exists() function in the sites
            // till we find the post, or till we run out of sites.
            $found = page_exists( $path );
    return $found; 

Note: switch_to_blog() can slow your site down if you've got a large network. You might want to look into how you can cache your results (perhaps using an option). Since that's pretty dependent on your specific situation, it's not addressed in this answer.


  • switch_to_site( $site->blog_id; should read: switch_to_site( $site->blog_id );
    – Mike Wells
    Jul 9, 2021 at 20:26
  • In fact, it should be switch_to_blog( $site->blog_id );. Good catch, thanks. I've fixed it.
    – Pat J
    Jul 10, 2021 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.