Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>";
  }
share|improve this question
1  
Please remember to come back and accept peoples answers. 0% is way too low. – Mild Fuzz Apr 27 '11 at 8:44
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

$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.
  }
}
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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