Comparing two stdClass Objects - php

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
(array) $variable;
Or, if your stdClass contains nested stdClasses, you can do the following:
function obj_to_array_recursive(stdClass $obj) {
foreach ($obj as &$element) {
if ($element instanceof stdClass) {
obj_to_array_recursive($element);
$element = (array)$element;
}
}
$obj = (array)$obj;
return $obj;
}

Related

PHP - Add multiple items by index to multidimensional array

I have this array called $all_cats which outputs the following
Array(
[0] => WP_Term Object(
[term_id] => 386
[name] => Ales
[slug] => ales
[term_group] => 0
[term_taxonomy_id] => 386
[taxonomy] => product_cat
[description] =>
[parent] => 384
[count] => 10
[filter] => raw
)
[1] => WP_Term Object(
[term_id] => 385
[name] => Beers
[slug] => beers
[term_group] => 0
[term_taxonomy_id] => 385
[taxonomy] => product_cat
[description] =>
[parent] => 384
[count] => 10
[filter] => raw
)
)
I'm trying to add the "term_id" and "name" to an indexed multidimensional array so i can output the following -
Example A
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 385,
[name] => "Beers"
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 386,
[name] => "Ales"
)
)
)
I've tried the following but cant seem to add each item to the same key. How can i add each term_id & name so it outputs like example A?
$full_cats = array();
foreach ($all_cats as $cat_term) {
$parent_termID = $cat_term->term_id;
$parent_title = $cat_term->name;
// this doesnt work
$full_cats[]['parent_cats']['id'] = $parent_termID;
$full_cats[]['parent_cats']['name'] = $parent_title;
// this doesnt work
array_push($full_cats[]['parent_cats']['id'],$parent_termID);
array_push($full_cats[]['parent_cats']['name'],$parent_title);
}
How can i add each term_id & name so it outputs like example A?
$full_cats = array();
foreach ($all_cats as $cat_term) {
$parent_termID = $cat_term->term_id;
$parent_title = $cat_term->name;
// this doesnt work
$full_cats[]=array(
"parent_cats"=>array(
"id" => $parent_termID,
"name" => $parent_title
)
);
}
The above code should work
You need to learn the structure of multi and assoc array
$full_cats = array();
$indx = 0;
foreach ($all_cats as $cat_term) {
$parent_termID = $cat_term->term_id;
$parent_title = $cat_term->name;
// this doesnt work
$full_cats[$indx]['parent_cats']['id'] = $parent_termID;
$full_cats[$indx]['parent_cats']['name'] = $parent_title;
$indx++;
// this doesnt work
// array_push($full_cats[]['parent_cats']['id'],$parent_termID);
// array_push($full_cats[]['parent_cats']['name'],$parent_title);
}

How to filter a category in wordpress/php?

I have a php code as shown below:
$category = get_the_category(); // Line #A
echo '<pre>'; print_r($category); echo '</pre>';// Line #B Added for debugging purpose
The code at Line#A retrieves post categories.
The code at the 2nd line which I have added for the debugging purpose returns the following array;
Array
(
[0] => WP_Term Object
(
[term_id] => 13085
[name] => Cannabis
[slug] => democracy_project_cannabis
[term_group] => 0
[term_taxonomy_id] => 13085
[taxonomy] => category
[description] => Hello World
[parent] => 13083
[count] => 8
[filter] => raw
[cat_ID] => 13085
[category_count] => 8
[category_description] => Good Morning
[cat_name] => Cannabis
[category_nicename] => democracy_project_cannabis
[category_parent] => 13083
)
[1] => WP_Term Object
(
[term_id] => 13093
[name] => Today
[slug] => today
[term_group] => 0
[term_taxonomy_id] => 13093
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 3
[filter] => raw
[cat_ID] => 13093
[category_count] => 3
[category_description] =>
[cat_name] => Today
[category_nicename] => today
[category_parent] => 0
)
)
Problem Statement:
I am wondering what php code I need to add after Line#A so that it takes only category [name] => Today.
The code at Line#A returns the list of categories for a specific post. I just want to take only one category.
I think I need to use array_filter() method but I am not sure how I can use it.
I think you are better off getting the posts you want based a specific category to begin with rather than filtering everything.
$term = get_term_by('name', 'Today', 'category');
if ($term) {
$category = get_the_category($term->term_id);
} else {
echo "Category not found";
}
You can loop through the returned $category array:
foreach($category as $cat) {
if ($cat->name == 'Today') {
//do your stuff
}
}

PHP check if value exists in multi-tier array

I have a PHP array, assigned to $terms, from Wordpress that looks like this when printed:
Array ( [0] => WP_Term Object (
[term_id] => 19
[name] => Tshirts
[slug] => tshirts
[term_group] => 0
[term_taxonomy_id] => 19
[taxonomy] => clothes
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[1] => WP_Term Object (
[term_id] => 30
[name] => Pants
[slug] => pants
[term_group] => 0
[term_taxonomy_id] => 30
[taxonomy] => clothes
[description] =>
[parent] => 0
[count] => 12
[filter] => raw
)
)
I am trying to use in_array() to check if a value exists within the array, but I am having trouble since the array contains more parameters than just the categories. Let's say I want to see if pants exists in this array, here's what I've tried:
if (in_array('pants', $terms)) {
echo "Pants in array";
}
How can I modify this if statement so that it checks if pants exists as a slug in this array?
You can use array_search and array_column in combination. You'll want to turn the WP_Term object to an array too. Luckily it has a to_array() method for that so we can use array_map to convert them en masse in our function before searching the items:
var_dump(termsContainsSlug('pants', $terms)); // bool(true)
function termsContainsSlug($slug, $terms) {
$terms = array_map(function($term) { return $term->to_array(); }, $terms);
return array_search($slug, array_column($terms, 'slug')) > 0;
}
First, you should transform each object to an array and then test if there are any 'pants' value in.
foreach($terms as $term) {
if (in_array('pants', (array)$term)) {
echo "Pants in array";
}
}
This should transform WP_Term Object -> array
(array)$term

Removing a value from an array. How can I get it to work

I'm trying to create a custom navigation bar by using the category names of a custom post type.
My approach is to create an array from the category names and create a navigation bar on the fly with the values from within the array. One of the category names is "uncategorized" and I want to get rid of that one.
The thing is that when I perform an array_search for 'uncategorized' nothing seems to be found and therefor nothing is unset. Can somebody shed some light on it?
The array and the partial code is below.
The array ($categories) looks like this:
[3] => WP_Term Object
(
[term_id] => 2
[name] => Logo
[slug] => logo
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 10
[filter] => raw
[cat_ID] => 2
[category_count] => 10
[category_description] =>
[cat_name] => Logo
[category_nicename] => logo
[category_parent] => 0
)
[4] => WP_Term Object
(
[term_id] => 1
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 1
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 7
[filter] => raw
[cat_ID] => 1
[category_count] => 7
[category_description] =>
[cat_name] => Uncategorized
[category_nicename] => uncategorized
[category_parent] => 0
)
My code so far looks like this:
<?php if ( get_post_type( get_the_ID() ) == 'work' ) {
$categories = get_categories( array() );?>
<!-- display the contents of the array before unset -->
<pre>
<?php print_r($categories);?>
</pre>
<!-- End display -->
<?php $key = array_search('uncategorized', $categories);
printf ($key);
unset($categories [$key]);
// display contents of the array after unset
foreach ( $categories as $category ) {
printf( $category->name );
// end display
}
}
?>
You can add an if in the foreach:
if($category->name != 'Uncategorized') {
printf($category->name);
}
That's it.

PHP working with arrays

I have this line of code:
print_r(get_the_terms( $_product->id, 'product_cat'));
which returns:
Array ( [0] => stdClass Object ( [term_id] => 67 [name] => Paintings [slug] => paintings [term_group] => 0 [term_taxonomy_id] => 67 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 44 ) [1] => stdClass Object ( [term_id] => 13 [name] => Small [slug] => small [term_group] => 0 [term_taxonomy_id] => 13 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 15 ) )
what I am trying to get is [name] => Paintings so I can create another array like so:
$array[get_the_terms( $_product->id, 'product_cat')->name] = $_product->get_title()
$_product->get_title() is "A Quiet Day"
expected output Array ( [Paintings] => A Quiet Day )
If I do this:
$array[] = $_product->get_title();
the output is
Array ( [0] => A Quiet Day )
I am just trying to replace the 0 with Paintings
$array['Paintings'] = $_product->get_title();
If you want the array to have specific keys, specify them...
The problem may be that it looks like the class you're storing that name in is not properly defined. Are you storing it in the session?
Your get_the_terms() function also appears to return more than one object, so you will not be able to chain ->name off it. You'll need to select the right one.
You need to make your code and question more readable.

Categories