Reputation: 131
I'm struggling to fix this myself or find answers so here I am. A tag exclusion function I've stitched together is also preventing custom posts with this tag from showing on its corresponding posts page in admin. How can I tweak the function to allow those custom posts to show up?
function exclude_post_by_tag( $query ) {
$excluded_tag_ids = array(47);
if ( $query->is_main_query() ) {
if ( ( $query->is_home() || $query->is_category() ||
$query->is_archive() || $query->is_feed() || $query->is_single() &&
!has_post_format( 'image' ) ) || ( !is_admin() &&
!$query->is_search() ) ) {
$query->set('tag__not_in', $excluded_tag_ids);
} else if ( $query->is_single() ) {
if ( ( $query->query_vars['p'] ) ) {
$page= $query->query_vars['p'];
} else if ( isset( $query->query_vars['name'] ) ) {
$page_slug = $query->query_vars['name'];
$post_type = 'post';
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
}
if ( $page ) {
$post_tags = wp_get_post_tags( $page );
foreach ($excluded_tag_ids as $tag_id ) {
if ( in_array( $tag_id, $post_tags ) ) {
$query->set( 'p', -$tag_id );
break;
}
}
}
}
}
}
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
The custom post type I'm using is called data
and the posts use a 'standard' post format.
Thanks so much for reading, I hope someone can help.
Upvotes: 1
Views: 67
Reputation: 131
I've just found the answer. I just needed to change:
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
to:
if( !is_admin() ){
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
}
to prevent it affecting the admin pages. Whew!
Upvotes: 0
Reputation: 6838
At the begin of your function, abort execution by checking if in admin context:
function exclude_post_by_tag( $query ) {
if( is_admin() )
return;
// rest of the code here
}
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
Upvotes: 2