[wp-trac] [WordPress Trac] #24612: Permalink conflict: image attachment page and post when permalink setting is /%postname%/

WordPress Trac noreply at wordpress.org
Sat Apr 18 00:35:52 UTC 2015


#24612: Permalink conflict: image attachment page and post when permalink setting
is /%postname%/
----------------------------------------+------------------------------
 Reporter:  augustas                    |       Owner:
     Type:  defect (bug)                |      Status:  reopened
 Priority:  normal                      |   Milestone:  Awaiting Review
Component:  Permalinks                  |     Version:  3.3
 Severity:  normal                      |  Resolution:
 Keywords:  has-patch needs-unit-tests  |     Focuses:
----------------------------------------+------------------------------

Comment (by SergeyBiryukov):

 A workaround:
 {{{
 function wp24612_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 ) {
                 // See if we also have a post with the same slug
                 $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',
 'wp24612_resolve_post_vs_attachment_permalink_conflict' );
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/24612#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list