0

I'm not certain I'm asking the right question but here goes...

$tags = get_tags(array('exclude' => 46,5,101,22,122,7,102,15,104,47,105,66,43,123, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;

When I echo this out via the last line it gives me a lovely list of comma separated numbers (the right numbers too fortunately). However, I don't want to echo them I want to include them in more code as follows...

$args = array(
'post_type' => 'post', 
'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
'tag__not_in' => array (comma separated list output by echo $tagString in same format as 'tag__in')
);

It has been suggested using explode but when I do that it returns...

Array ( [0] => 10 [1] => 121 [2] => 20 [3] => 36 etc etc)

I need to lose all the formatting and just get the comma separated list.

Possibly I'm approaching this wrong and maybe I'm not making sense but hopefully someone can follow what I'm trying to achieve. Any help appreciated.

It is linked to this question which got me this far... Trouble including array output in another array

Update:

Thank you James who put me on the right lines. This is the code that did the trick...

$includeTags = array(46,5,101,22,122,7,102,15,104,47,105,66,43,123);
$excludeTags = get_tags(array('exclude' => $includeTags, 'fields' => ids));

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12,
    'paged' => $paged,
    'tag__in' => $includeTags,
    'tag__not_in' => $excludeTags
);

I've changed the array names for clarity.

4 Answers 4

1

I'm definitely not sure what you are asking. First, I'm surprised your original array works at all. You are using a mix between an associative array and just an incremental array it looks like.

Can you try something like:

$excludeArr = array(46,5,101,22,122,7,102,15,104,47,105,66,43,123);
$tags = get_tags(array('exclude' => $excludeArr, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;

Where the exclude is an array itself. Then:

$args = array(
'post_type' => 'post', 
'tag__in' => $excludeArr,
'tag__not_in' => $excludeArr
);

None of that $args array makes much since since the in and not_in appear to be the same, though.

If you just need $excludeArras a string, you can implode(",", $excludeArr);

EDIT

After seeing your update, I'm editing my answer as requested to show the 2 seperate arrays.

$includeTags = array(46,5,101,22,122,7,102,15,104,47,105,66,43,123);
$excludeTags = get_tags(array('exclude' => $includeTags, 'fields' => ids));

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12,
    'paged' => $paged,
    'tag__in' => $includeTags,
    'tag__not_in' => $excludeTags
);
Sign up to request clarification or add additional context in comments.

4 Comments

Just re in and not_in - they are basically opposites. in is all the ids I list manually as they are fixed. not_in will change as new tags / ids are added. I'm therefore trying to calculate it by using the get_tags function which has the ability to get_tags except the tag ids I list to be excluded (is a Wordpress function). Therefore in includes the tag ids I list and not_in is basically all the tags except $tags \ $tagString
James, just to let you know your code basically put me on the right lines and I have it working exactly as I need. On that basis, I will add my code to the question and if you would like to edit your answer I can tick it. I know my question was confusing.
@PaulBrown I see your update, but where does your question stand now? Can you add what is still not working for you? Is it that you need both the exclude and include arrays to be strings?
Hi @James no - I felt that my code was really just a reworked version of the code you provided but my question confused you a little. There is nothing outstanding - the code I added to the bottom of my question works perfectly but I didn't want to add it as an answer as I felt it was close to plagiarizing you. On that basis would you like to either edit your answer or add a new one so that I can tick it?
1

for block 2 cant you just use

$args = array(
'post_type' => 'post', 
'tag__in' => $tagString,
);

4 Comments

There should be no ending comma after $tagString in this example. Tried to edit but I guess it didn't go through.
@James its perfectly valid to have a comma, not required, but valid and preferred by some of us (is codding not grammar :-))
I guess you are right. I just tested it. Could have swore it threw an error on me when I had done that previously saying it was expecting key/values after the comma. Ah well.
Thank you but no because block 2 basically says include posts that are tagged with any of (tag__in ids) but if they are also tagged with any of (tag__not_in ids) then they should be excluded. $tagString is basically calculating all the tag ids that aren't included in tag__in so it returns all ids except 46,5,101, etc
0

Your best friend is named

serialize()

(http://www.php.net/manual/de/function.serialize.php)

This simply converts the whole array contents to a string. :)

1 Comment

Thank you - is 1am here in UK but will look at that in more detail tomorrow. Appreciate the signpost.
0
$includeTags = array(46,5,101,22,122,7,102,15,104,47,105,66,43,123);
$excludeTags = get_tags(array('exclude' => $includeTags, 'fields' => ids));

$args = array(
'post_type' => 'post', 
'posts_per_page' => 12,
'paged' => $paged,
'tag__in' => $includeTags,
'tag__not_in' => $excludeTags
);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.