Skip to content

Instantly share code, notes, and snippets.

@leobaiano
Created October 11, 2016 13:38
Show Gist options
  • Save leobaiano/aef02b1ed30f2ab132e7d664435f89b1 to your computer and use it in GitHub Desktop.
Save leobaiano/aef02b1ed30f2ab132e7d664435f89b1 to your computer and use it in GitHub Desktop.
[WordPress] - It allows you to use CPT and pages relative
add_action('parse_request', 'cpt_parent_page' );
function cpt_parent_page( $wp ) {
if ( isset ( $wp->query_vars['attachment'] ) && ! empty( $wp->query_vars['attachment'] ) ) {
$page = get_page_by_path(
$wp->request,
OBJECT,
array( 'page', 'book' )
);
if ( $page instanceof WP_Post ) {
$wp->query_vars = [ 'page_id' => $page->ID ];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment