0

Difficult to explain and maybe totally impossible but I have say 30 tags in total. I would like to output all posts that contain at least one from a list of say 10 tags but do not contain any of the other 20.

So for example (assuming I'm targeting posts exclusively containing any number of tags one - ten):

Post containing tags: one three eight ten - would be output Post containing tags: one three eight ten twelve - would not be output

When I say output it could be a list to include in a post or a unique post / page with its own template. The output method I am flexible with - it's targeting the posts I'm not sure can be done? I am only talking about the tag taxonomy - no other variables.

========================================

Update 16 Jan 2014:

I have created a new template, the relevant parts of which are:

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12,
    'paged' => $paged,
    'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
    'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)

);

$wp_query = null;
$wp_query = new WP_Query();
$wp_query -> query($args);
?>

<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

<?php get_template_part('includes/format', 'archive-update'); ?>

<?php endwhile; ?>
<?php endif; ?>

This works well - the issue is that the tag__in array will be static but the tag__not_in could be dynamic as new tags are added. I would like to automate this array (if that is the correct phrase). To grab the tag ids quickly for tag__not_in I created another template with the following code:

$tags = get_tags('exclude=46,5,101,22,122,7,102,15,104,47,105,66,43,123');

$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {

    $html .= "{$tag->term_id},";
}
$html .= '</div>';
echo $html;

and then copied and pasted the displayed ids manually into the tag__not_in array.

Basically how can I adapt the line:

'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)

so that the ids are automatically populated by the array created by get_tags(exclude...). I hope that makes sense.

The output page is http://skipology.com/iphone-photography-articles-10-essential-apps/

6
  • try this codex.wordpress.org/… Commented Jan 15, 2014 at 11:17
  • Thank you Brad. I've had a read and also been doing more research. Possibly: $query = new WP_Query( array( 'tag__in' => array( 37, 47 ) ) ); for the tags I want included combined with: $query = new WP_Query( array( 'tag__not_in' => array( 37, 47 ) ) ); to ensure the posts don't also have other tags but: 1. How do I combine them in to a single query 2. Re the tag__not_in array is there any way to say all except those listed in the tag__in array? If I list manually I would need to update the code if new tags are added! codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
    – Paul Brown
    Commented Jan 15, 2014 at 16:47
  • I'm getting there slowly all I need help with now is... 2. Re the tag__not_in array is there any way to say all except those listed in the tag__in array? If I list manually I would need to update the code if new tags are added! Any ideas / pointers?
    – Paul Brown
    Commented Jan 15, 2014 at 21:02
  • You could use a conditional tag. !has_tag( array( 'sharp', 'mild', 'extreme' ) ) Returns true when a post doesn't include any of these tags. Commented Jan 15, 2014 at 23:38
  • Thank you Brad, I've got some code and I've been trying to use: $tags = get_tags('exclude=46,5,101,22,122,7,102,15,104,47,105,66,43,123'); to return all the tags except those listed. I've then tried to insert the variable into my code via... 'tag__not_in' => $tags but it doesn't work. I think this is my inexperience in coding and is missing a step or two to get tag not in to recognise what $tags is. Not sure.
    – Paul Brown
    Commented Jan 16, 2014 at 0:06

1 Answer 1

0

Paste this code at the end of your child themes functions.php file.

It will exclude all posts with the comma separated list of tag i.d's from displaying on the home page loop.

add_filter('request', 'filter_tag_loop');
function filter_tag_loop($params) {
$params['tag__not_in'] = array(49);
return $params;
}

Or you could use WP_Query

$query = new WP_Query( array( 'tag__not_in' => array( 46,5,101,22,122,7,102,15,104,47,105,66,43,123 ) ) );

http://codex.wordpress.org/Class_Reference/WP_Query

Or use pre_get_posts (untested)

add_action( 'pre_get_posts', 'wpsites_include_exclude_tagged_posts' );

function wpsites_include_exclude_tagged_posts( $query ) {

if ( $query->is_main_query()  && $query->is_home() ) {

    $query->set( 'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123)),
    $query->set( 'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85));
    }
}

http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

2
  • Thank you Brad, really appreciate your help. I think I'm nearly there. I'm going to post another answer and would be grateful if you or anyone else can help with the final finishing touch.
    – Paul Brown
    Commented Jan 16, 2014 at 9:46
  • In fact - I will edit my original question with my updated code - be grateful if anyone could take a look.
    – Paul Brown
    Commented Jan 16, 2014 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.