That’s strange issue I have on this one website. Similar to this problem and I am trying to get this fixed from last couple of weeks.

This website have latest WordPress installed and /%postname%/ permalink structure.

Sometimes, when I try to view a post, I get redirected to attachment post /?attachment_id=358 instead of the post itself. This happens on posts when post and the attachment have the same title.

Although I can make the post working again if I change the permalink of the post. But that’s too late since this website publish and share posts automatically. And users get error when they do not get to the right post.

I already tried disabling all plugins for a while. None worked.

1 Answer
1

I think the reason for this is the get_page_by_path() check inside the WP_Query, because it’s checking for both page and attachment slugs, in this case.

So if we have both an attachment and a post with the someslug postname and visit:

example.tdl/someslug

then get_page_by_path() will generate the following query:

SELECT ID, post_name, post_parent, post_type
FROM wp_posts
WHERE post_name IN ('someslug')
AND post_type IN ('page','attachment')

and we’re therefore served the attachment’s page instead of the post’s page.

So this seems to be a core feature but not a bug generated by your plugins.

Workaround

Because of the permalink settings, the post request is handled like a page request.

We therefore have the pagename query variable constructed instead of the name query variable.

We can adjust the query variables, generated for the post request:

/**
 * Handle post requests correctly, in the case of the /%postname%/ permalink settings
 */
! is_admin() && add_filter( 'request', function( $query_vars )  
{
    if( 
           isset( $query_vars['pagename'] ) 
        && $post = get_page_by_path( $query_vars['pagename'], OBJECT, 'post' )
    ) {
        if( is_a( $post, '\WP_Post' ) )
        {
            // Add the 'name' query variable 
            $query_vars['name'] = $post->post_name;     

            // Unset the 'pagename' query variable
            unset( $query_vars['pagename'] );
        }
    }
    return $query_vars;
}, PHP_INT_MAX );

Here we check if a post exists with the pagename slug and if it exists, we construct the name query variable and finally unset the pagename query variable.

Now the post should be served instead of the attachment.

Leave a Reply

Your email address will not be published. Required fields are marked *