Make WordPress Core

Opened 10 years ago

Last modified 5 years ago

#31957 new defect (bug)

query fails, when post is queried under specific name

Reported by: selnomeria's profile selnomeria Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1.1
Component: Query Keywords:
Focuses: Cc:

Description

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;
	}
}	

Change History (4)

#1 @selnomeria
10 years ago

Also, if i access:
yoursite.com/cars/subcat/

then it should not try to find, if a standard category exists under that slug (instead of showing 404, as it think that it's custom post type page)

Version 0, edited 10 years ago by selnomeria (next)

#2 @selnomeria
10 years ago

sorry, same problem for 'pages' ,not only posts.. so, the same custom query is needed---

//PAGE exists... with the found slug("TORNADOO"), lets check if it's a really page..
$page=get_page_by_path('cars/subcat/TORNADOO', OBJECT, 'page');
if ($page){
	//new query
	$query->init();
	$query->parse_query(   array('post_type' =>'page')  ) ;	
	//others
	$query->is_home = false;
	$query->is_single = false;
	$query->is_singular = true;
	$query->is_page = true;
	$query->queried_object_id = $page->ID; 
	$query->set( 'page_id', $page->ID );
	return;
}
Last edited 10 years ago by selnomeria (previous) (diff)

#3 @DrewAPicture
9 years ago

  • Component changed from General to Query

This ticket was mentioned in Slack in #core by boone. View the logs.


8 years ago

Note: See TracTickets for help on using tickets.