I'm currently working on a modified search within a Wordpress theme that queries a custom taxonomy location
and displays it's terms as search results. I couldn't find a built in Wordpress function to handle this, so I used a $wpdb query
$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>
The issue with this is the table wp_terms
not only stores custom taxonomy terms, but other default terms as well. So in order to display search results just for the the custom taxonomy, not other default terms, I thought of using get_terms
to get all of the terms belonging to the custom taxonomy location
and display terms from the table wp_terms
based off of get_terms
result with in_array
$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>
However, $results
and $terms
are both stdClass Objects, so in_array
doesn't work.
Is there either a function, method or possibly a MySQL query that will allow me to display results from the object $results
based off the contents of the object $terms
?
Thanks in advance.
EDIT:
Contents of $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 )
)
You can convert them into arrays with
Or, if your stdClass contains nested stdClasses, you can do the following: