MIKROM: zkus
function resolve_post_vs_attachment_permalink_conflict( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( ! preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) {
return;
}
if ( empty( $query->queried_object ) || '' === $query->get( 'pagename' ) ) {
return;
}
if ( 'attachment' === $query->queried_object->post_type ) {
$post = get_page_by_path( $query->get( 'pagename' ), OBJECT, 'post' );
if ( $post ) {
$query->queried_object = $post;
$query->queried_object_id = (int) $post->ID;
$query->is_page = false;
$query->is_single = true;
}
}
}
add_action( 'parse_query', 'resolve_post_vs_attachment_permalink_conflict' );