Skip to content

Instantly share code, notes, and snippets.

@stephanieleary
Created April 27, 2016 16:11
Show Gist options
  • Save stephanieleary/c725ea4620e9d434e71927ab41879f38 to your computer and use it in GitHub Desktop.
Save stephanieleary/c725ea4620e9d434e71927ab41879f38 to your computer and use it in GitHub Desktop.
infer post type when query var is not set
<?php
function berkeley_find_post_type() {
$type = get_query_var( 'post_type' );
if ( ( !isset( $type ) || empty( $type ) ) && is_tax() ) {
// we're on a term archive, where post type might not be set but can be inferred from taxonomy's object_type
$current_term = get_queried_object();
$tax_obj = get_taxonomy( $current_term->taxonomy );
if ( count( $tax_obj->object_type ) == 1 )
$type = $tax_obj->object_type[0];
}
return $type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment