我正在运行一个使用wpml的wordpress网站,这会给我一些问题,试图对我在翻译过的帖子中的标签运行查询。
所以我想要做的是一个小小的黑客,这将允许我查询所有帖子,并使用wordpresses函数get_the_tags();
找到具有特定标签的帖子我正在尝试使用in_array,它似乎不适用于wordpress输出的多维数组,这里是来自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
> )
>
> )
这是我的代码我只希望它找到名称为welsh1的数组,这是数组中的最后一个。
// 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)
$ tags是一个对象数组,而不是多维数组。
以下代码应标识字符串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.
}
}