I'm running into a catch-22 situation running a request filter. I need the request to be correct in order to run the correct filter, but I need the filter to correct the request. GAH!
Here is the code:
function cpt_parse_taxonomy_string( $request, $cpt, $ctax ){
if( array_key_exists( $ctax , $request )
&& ! get_term_by( 'slug', $request[$ctax], $ctax ) ){
$request[$cpt] = $request[$ctax];
$request['name'] = $request[$ctax];
$request['post_type'] = $cpt;
unset( $request[$ctax] );
}
return $request;
}
function cpt_request_filters( $request ) {
global $post;
if ( 'toys' == get_post_type( $post ) ) {
return cpt_parse_taxonomy_string( $request, 'toys', 'toy_series' );
}
if ( 'books' == get_post_type( $post ) ) {
return cpt_parse_taxonomy_string( $request, 'books', 'book-publishers-series' );
}
return $request;
}
add_filter( 'request', 'cpt_request_filters' );
In an effort to keep my code DRY and not do a redundant filter for every post-type that will be affected, I tried to push the bulk of the code into a reusable function. However, the function is never being run.
The second function is where I'm having the difficulty. I am attempting to call in the global $post, but I think I am trying to get it too early. It doesn't exist yet, so it's coming in null which makes the rest of my conditionals flake and the first function never runs for either custom post type I am testing for. Then I thought perhaps I could look at the request itself to get the info I need, but because the filter hasn't run yet, the request is potentially wrong and my conditionals will not run correctly.
How can I get the post or what other way can I test for those post types?
Thanks!