0
我目前正在研究Wordpress主题中的修改搜索,该主题查询自定义分类标准location
并将其术语显示为搜索结果。我无法找到一个内置在WordPress的功能,以处理这个问题,所以我用一个$wpdb query
比较两个stdClass对象
$keywords = $_GET['s'];
$results = $wpdb->get_results("SELECT * FROM $wpdb->terms WHERE name LIKE '%%$keywords%%'");
<ul>
<?php foreach ($results as $result) :?>
<li><?php echo $result->name;?></li>
<?php endforeach;?>
</ul>
的问题与此是表wp_terms
不仅存储自定义分类方面,但其他默认条款以及。因此,为了显示搜索结果只是为自定义分类,而不是其他默认的条款,我想用get_terms
获得基于关闭的get_terms
结果与in_array
都属于自定义分类从表wp_terms
location
和显示方面的条款的
$keywords = $_GET['s'];
$results = $wpdb->get_results("SELECT * FROM $wpdb->terms WHERE name LIKE '%%$keywords%%'");
$terms = get_terms("location");
<ul>
<?php foreach ($results as $result) :?>
if(in_array($result->name, $terms)) :?>
<li><?php echo $result->name;?></li>
<?php endif;?>
<?php endforeach;?>
</ul>
然而,$results
和$terms
都是stdClass的对象,所以in_array
不起作用。
是否有一个函数,方法或MySQL查询,这将允许我显示对象$results
的结果基于对象的内容$terms
?
在此先感谢。
编辑: 内容的$terms
Array (
[0] => stdClass Object ([term_id] => 32 [name] => US [slug] => us [term_group] => 0 [term_taxonomy_id] => 32 [taxonomy] => signs [description] => [parent] => 25 [count] => 1)
[1] => stdClass Object ([term_id] => 22 [name] => EU [slug] => eu [term_group] => 0 [term_taxonomy_id] => 22 [taxonomy] => signs [description] => [parent] => 0 [count] => 3)
[2] => stdClass Object ([term_id] => 26 [name] => AU [slug] => au [term_group] => 0 [term_taxonomy_id] => 26 [taxonomy] => signs [description] => [parent] => 22 [count] => 1)
[3] => stdClass Object ([term_id] => 27 [name] => IE [slug] => ie [term_group] => 0 [term_taxonomy_id] => 27 [taxonomy] => signs [description] => [parent] => 22 [count] => 2)
[4] => stdClass Object ([term_id] => 23 [name] => PK [slug] => pk [term_group] => 0 [term_taxonomy_id] => 23 [taxonomy] => signs [description] => [parent] => 0 [count] => 2)
)
感谢您的哟你的帮助,我用'obj_to_array_recursive($ terms)'调用函数;'得到'致命错误:调用未定义的函数obj_to_array_recursive()'。不知道如果我正确地调用它,有什么建议吗? – dcd0181
@ dcd018:您在使用它之前是否定义了该功能?这不是一个PHP本地函数,它是我写的函数... –