1

I have the following code:

$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;

Which echos out as... 10,121,20,36,23,66,24,21,105,76,82,17,22,122,43,47,102,5,6,106,8,75,54,38,57,86,56,101,123,95,25,62,16,39,40,69,37,9,42,7,15,41,87,73,85,104

This is great. However, I actually want to include the result (which I guess is $tagString) in another array as follows...

$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 ($tagString)
);

I've tried removing brackets adding single / double / no quotes, removing the word 'array' in front of $tagString across all combinations but it just doesn't work. When I manually create...

'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)

The code works perfectly. How can I get the output from $tagString to be the content of 'tag__not_in' array within the brackets? Is this possible?

========

Update to reflect Amal's code...

$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;

$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)*/
    'tag__not_in' => explode(',', $tagString)
);

1 Answer 1

3

$tagString is a string - simply inserting it in array(...) won't create an array (unless you use eval(), which is generally a bad idea). Simply use explode() to create an array from the comma-separated string, like so:

'tag__not_in' => explode(',', $tagString)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Amal, thank you my friend. I have tried it (and included the full code block in my question) but it outputs up to the echo $tagString; and then nothing. I've tried as before adding brackets and inserting array before explode and surrounding it with brackets but it either results in the code being ignored completely or returning nothing. Can you see anything else there? Many thanks.
I think I've found the problem @Amal - I've added print_r and it is returning the following... Array ( [0] => 10 [1] => 121 [2] => 20 [3] => 36 [4] => 23 [5] => 66 [6] => 24 [7] => 21 [8] => 105 [9] => 76 [10] => 82 [11] => 17 [12] => 22 [13] => 122 [14] => 43 ... ) I need it to return a comma separated list such as... Array (10,121,20,36,23,66,24,21,105,76,82,17,22,122,43 ... ) i.e. lose the... [x] => and include a comma instead?

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.