1

I created an MU Plugin which will only display the posts from certain tags in the loop like this:

function custom_tags( $query ) {
    $query->set( 'tag', array( 'custom', 'general' ) );
}
add_action( 'pre_get_posts', 'custom_tags' );

It works fine when I remove the array and only check for 1 tag, but how do I get it to work with more than 1 tag like I'm trying to above?

The Error I'm getting is:

Warning: strpos() expects parameter 1 to be string, array given in /srv/users/s/wp-includes/query.php on line 1966

Warning: preg_split() expects parameter 2 to be string, array given in /srv/users/s/wp-includes/query.php on line 1967

Updated Code:

$current = substr($_SERVER[HTTP_HOST], 0, -4);

function custom_tags( $query ) {
        $query->set( 'tag', 'general,{$current}' );
}
add_action( 'pre_get_posts', 'custom_tags' );
1
2

As Milo ( and your errors ) point out: you're passing an array where a string is expected. According to WP_Query tag parameter

Show posts associated with certain tags.

  • tag (string) - use tag slug.

To get around this you just need to pass a comma separated string:

function custom_tags( $query ) {
    $query->set( 'tag', 'custom,general' );
}
add_action( 'pre_get_posts', 'custom_tags' );
8
  • @howdy-mcgee this works to check for 2 or more tags thanks ! if i wanted to add a dynamic tag as one of the possible tags how would i alter your code to do this? the variable is $current – Joe Bobby Dec 15 '15 at 22:44
  • @JoeBobby in PHP you can append variables to strings using double quotes, like this: "custom,general,{$current}". – Howdy_McGee Dec 15 '15 at 22:46
  • @howdy-mcgee I updated the bottom of my post with the updated code but i still cant get the dynamic tag to work, basically what its suppose to do is generate the domain name as domain.com and remove the last 4 characters which will remove the TLD and period – Joe Bobby Dec 15 '15 at 22:54
  • @JoeBobby in PHP single quotes 'like this' define absolute strings which means everything inside of it will be treated as a string. On the other hand, double quotes "like this" create a string where you can process variables. You need to replace your single quotes with double quotes. You may also want to move your $current variable inside your function call. – Howdy_McGee Dec 15 '15 at 22:56
1

Create a proper tax_query, for example:

'tax_query' => array(
    array(
        'taxonomy' => 'people',
        'field'    => 'slug',
        'terms'    => 'bob',
    ),
),

But your code "update code" is going to fail for other reasons too.

  1. $current is out of scope.
  2. And your variable won't expand inside single quotes

The bare bones changes you need are:

function custom_tags( $query ) {
  $current = substr($_SERVER[HTTP_HOST], 0, -4);
  $query->set( 'tag', "general,{$current}" );
}
add_action( 'pre_get_posts', 'custom_tags' );

But as mentioned, I'd create a proper tax_query

function custom_tags( $query ) {
  $current = substr($_SERVER[HTTP_HOST], 0, -4);
  $tax = array(
    array(
        'taxonomy' => 'tag',
        'field'    => 'slug',
        'terms'    => $current,
        'operator' => 'IN' // This is default
      ),
    );
  $query->set( 'tag', "general,{$current}" );
}
add_action( 'pre_get_posts', 'custom_tags' );

The operator can be changed to get different behavior:

operator (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS'. Default value is 'IN'.

And your code is going to run on every query on the site, just about. That is going to alter a lot of things, and will certainly break things. You need to restrict it to only those locations you need it. I don't know exactly where or when this is supposed to run but this should be a start:

function custom_tags( $query ) {
  if (is_admin() 
    || $query->is_main_query()
  ) {
    return;
  }
  $current = substr($_SERVER[HTTP_HOST], 0, -4);
  $tax = array(
    array(
        'taxonomy' => 'tag',
        'field'    => 'slug',
        'terms'    => $current,
        'operator' => 'IN' // This is default
      ),
    );
  $query->set( 'tag', "general,{$current}" );
}
add_action( 'pre_get_posts', 'custom_tags' );
2
  • @s-ha-dum getting Parse error: syntax error, unexpected '||' (T_BOOLEAN_OR) in /srv/users/s/wp-content/mu-plugins/c.php on linloope 24 and after removing the || i get another error so not sure how to fix – Joe Bobby Dec 16 '15 at 1:16
  • There is a extra parents after is_abmin() – s_ha_dum Dec 16 '15 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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