[wp-trac] [WordPress Trac] #31957: query fails, when post is queried under specific name

WordPress Trac noreply at wordpress.org
Mon Apr 13 07:28:12 UTC 2015


#31957: query fails, when post is queried under specific name
--------------------------+-----------------------------
 Reporter:  selnomeria    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  4.1.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 i.e., i have custom post type, named "CARS",
 and when i have standard category, named "CARS"..

 when i publish '''standard''' post  under "cars" category:
 mysite.com/cars/something-name...

 worpdress thinks, that it is under custom-post types, and 404 page is
 displayed..

 to fix that, i have made this function:

 {{{

 //example URL:    yoursite.com/cars/subcat/TORNADOO
 add_action( 'pre_get_posts', 'myf' ); function myf( $query ) {
     if ( $query->is_main_query() ) {
         //when the PERMALINK was not found, then 404 maybe triggered.. But
 wait! maybe it is a standard post, under the standard category(which's
 name is i.e. "cars")
         if (!url_to_postid(currentURL__MLSS)){
                 $link_array=explode('/','/cars/subcat/TORNADOO');
                         $k=array_filter($link_array);   //remove empty
 values
                         $k=array_values($k);                    //reset
 hierarchy
                         $all_nmb = count($k);

                 //if post exists with the found slug("TORNADOO"), lets
 check, if their parents are categories
                 $post=get_page_by_path(basename('TORNADOO'), OBJECT,
 'post');
                 if ($post){
                         for($i=0; $i<$all_nmb-1; $i++){
                                 $cat = get_term_by('slug', $k[$i],
 'category');
                                 if(!(in_category($cat->term_id,$post->ID)
 || post_is_in_descendant_category(array($cat->term_id),$post->ID))){
                                         $failed=true; break;
                                 }
                         }
                         if (!$failed){
                                 //new query
                                 $query->init();
                                 $query->parse_query(   array('post_type'
 =>'post')  ) ;
                                 //others
                                 $query->is_home = false;
                                 $query->is_single = true;
                                 $query->is_singular = true;
                                 $query->is_page = false;
                                 $query->queried_object_id = $post->ID;
                                 $query->set( 'page_id', $post->ID );
                         }
                 }
         }

         //if page exists with the that link, then query will be made
 correctly..
         //$page=get_page_by_path(...., ..., 'page');
 }









 if ( ! function_exists( 'post_is_in_descendant_category' ) ) { function
 post_is_in_descendant_category( $cats, $_post = null ) {
                 foreach ( (array) $cats as $cat ) {     $descendants =
 get_term_children( (int) $cat, 'category' );
                         if ( $descendants && in_category( $descendants,
 $_post ) ) {return true;}
                 }return false;
         }
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31957>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list