At this moment I am using the code below, to add a shortcode for a table on all the posts and pages, but I would like to exclude few pages and any post with a specific tag. (ex. page ids 10,20 and tag id 30)
I am quite new to all of this, and I am still learning.
function my_shortcode_to_a_post( $content ) {
global $post;
if( ! $post instanceof WP_Post ) return $content;
switch( $post->post_type ) {
case 'post':
return $content . '[table id=1 /]';
case 'page':
return $content . '[table id=1 /]';
default:
return $content;
}
}
add_filter( 'the_content', 'my_shortcode_to_a_post' );
Thanks for the help in advance.
Please try this and let me know the result
function my_shortcode_to_a_post( $content ) {
global $post;
$disabled_post_ids = array('20', '31', '35');
$disabled_tag_ids = array('5', '19', '25');
$current_post_id = get_the_ID(); // try setting it to $post->ID; if it doesn't work for some reason
if(in_array($current_post_id, $disabled_post_ids)) {
return $content;
}
$current_post_tags = get_the_tags($current_post_id);
if($current_post_tags){
$tags_arr = array();
foreach ($current_post_tags as $single_tag) {
$tag_id = $single_tag->term_id;
if(in_array($tag_id, $disabled_tag_ids)) {
return $content;
}
}
}
switch( $post->post_type ) {
case 'post':
return $content . '[table id=1 /]';
case 'page':
return $content . '[table id=1 /]';
default:
return $content;
}
}
add_filter( 'the_content', 'my_shortcode_to_a_post' );