0

i am running a wordpress website that use wpml which is giving me loads of issues trying to run a query on tags on my translated posts which are in welsh cy.

So what i am trying to do is a little hack that will allow me to query all posts and locate the post with a specific tags using wordpresses function get_the_tags();

I am trying to use in_array which doesnt seem to work on the multidimesional array that wordpress outputs, here is the array from a print_r();

>   Array (
>     [629] => stdClass Object
>         (
>             [term_id] => 629
>             [name] => bulletin
>             [slug] => bulletin
>             [term_group] => 0
>             [term_taxonomy_id] => 630
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 2
>             [object_id] => 19838
>         )
> 
>     [631] => stdClass Object
>         (
>             [term_id] => 631
>             [name] => english2
>             [slug] => english2
>             [term_group] => 0
>             [term_taxonomy_id] => 632
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>             [object_id] => 19838
>         )
> 
> ) Array (
>     [629] => stdClass Object
>         (
>             [term_id] => 629
>             [name] => bulletin
>             [slug] => bulletin
>             [term_group] => 0
>             [term_taxonomy_id] => 630
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 2
>             [object_id] => 19842
>         )
> 
>     [630] => stdClass Object
>         (
>             [term_id] => 630
>             [name] => english1
>             [slug] => english1
>             [term_group] => 0
>             [term_taxonomy_id] => 631
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>             [object_id] => 19842
>         )
> 
> ) Array (
>     [0] => stdClass Object
>         (
>             [term_id] => 633
>             [name] => welsh2
>             [slug] => welsh2
>             [term_group] => 0
>             [term_taxonomy_id] => 634
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>         )
> 
> ) Array (
>     [0] => stdClass Object
>         (
>             [term_id] => 632
>             [name] => welsh1
>             [slug] => welsh1
>             [term_group] => 0
>             [term_taxonomy_id] => 633
>             [taxonomy] => post_tag
>             [description] => 
>             [parent] => 0
>             [count] => 1
>         )
> 
> )

And here is my code i only want it to located the array with the name welsh1 which is the last one in the array.

  // Global calls to the database
  global $wpdb;

  // Runs a query to get all results from the wp_posts table
  $all = $wpdb->get_results( "SELECT * FROM wp_posts" );

  // loops through each one   
  foreach($all as $v){

      $tags = get_the_tags($v->ID);

      if (in_array('welsh1', $tags)) {
        echo "'ph' was found\n";
      }

      echo "<pre>";
      print_r($tags);
      echo "</pre>";
  }
0

1 Answer 1

1

$tags is an array of objects, not a multidimensional array.

The following code should identify the string welsh1

foreach($tags as $tag){
  if ($tag->name == "welsh1" || $tag->slug == "welsh1"){
     echo "'ph' was found\n";
     break;//this line makes the foreach loop end after first success.
  }
}

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.