Simple PHP out of Wordpress Array / Object - php

Here is my code
$getcatid = get_the_category($id);
$postisin = $getcatid;
echo print_r($postisin);
This returns:
Array (
[0] => stdClass Object (
[term_id] => 8
[name] => High Net Worth Individuals
[slug] => high-net-worth-individuals
[term_group] => 0
[term_taxonomy_id] => 8
[taxonomy] => category
[description] =>
[parent] => 7
[count] => 1
[object_id] => 1266
[cat_ID] => 8
[category_count] => 1
[category_description] =>
[cat_name] => High Net Worth Individuals
[category_nicename] => high-net-worth-individuals
[category_parent] => 7
)
) 1
All that I want to do is grab the [term_id] and assign it as a variable to be used later.
In this case, '8' I'm not the best with PHP, but it's been a while since I've worked with Arrays / Objects and I've been unable to find a solution. I have also lost my Lynda login information > < Help please?

You can get it like this:
$postisin[0]->term_id

use loop
foreach ( $postisin as $rows ) {
echo $rows->term_id;
}
or if you want to echo only one index you can use
echo $postision[0]->term_id;

Try this:
$termId = $postisin[0]->term_id;

Related

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
}
}

Display a single element of an array

I think it is a very simple question but for a long time I am trying to figure this out:
I need to store the number 18 ([term_id]) from the following array in a variable, how can I do this?
Array (
[0] => WP_Term Object (
[term_id] => 18
[name] => Im Rebgarten
[slug] => im-rebgarten
[term_group] => 0
[term_taxonomy_id] => 18
[taxonomy] => give_forms_category
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[object_id] => 900
)
)
Kind regards
You can simply access the variable from object by -> notation like
$array = array();//Your current array
$term_variable = $array[0]->term_id; //store the term_id into the variable
print_r($term_variable); //prints the variable

fastest way to get array block based on value contained within the block (not working)

https://gist.github.com/jcranny/9465715
I have this array (example)...
Array
(
[0] => Array
(
[series] => stdClass Object
(
[term_id] => 5
[name] => Moto2 2013
[slug] => moto2-2013
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 77
[team] => Technomah carXpert
[constructor] => Suter
[machine] => Honda CBR600RR
)
[1] => Array
(
[series] => stdClass Object
(
[term_id] => 6
[name] => Moto2 2014
[slug] => moto2-2014
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 15
[team] => La Fonte Tascaracing
[constructor] => Suter
[machine] => Honda CBR600RR
)
[2] => Array
(
[series] => stdClass Object
(
[term_id] => 7
[name] => Moto2 2015
[slug] => moto2-2015
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 15
[team] => Mapfre Aspar Team Moto2
[constructor] => Suter
[machine] => Honda CBR600RR
)
)
Now I would like to be able to get information from each block.
For example I would like to echo this data:
[race_number]
[team]
[constructor]
[machine]
But I want only to echo this data that is relevant to a specific [series]
I have the [series] term_id so this is my key to get the relevant data, but i'm struggling to get my node function work.
This is the function to do this:
function node_modify_riders_array($rider_id)
{
$fields = get_field("rider_series_data", $rider_id);
foreach($fields as $field_key => $field_val)
{
$new_fields[$field_val["series"]] = $field_val;
}
return $new_fields;
}
Then this is how I am get the series specific data based on the series term id.
$rider_series_data = node_modify_riders_array($rider_id);
$series_id = $series[0]->term_id;
$team = $rider_series_data[$series_id]["team"];
$contstructor = $rider_series_data[$series_id]["constructor"];
$machine = $rider_series_data[$series_id]["machine"];
$race_number = $rider_series_data[$series_id]["race_number"];
But some thing is wrong and I can work it out. Can anyone see where I'm going wrong or help me fix it.
Massive thanks in advance, would really appreciate some help.
What the problem is:
My function node_modify_riders_array is returning null, which is causing my $team, $contstructor, $machine, and $race_number too not output anything.
If I echo $series_id on my example, I get 6
Which should pass though my node_modify_riders_array function and display the relevant array values. But it's not outputting anything and I got no errors.
This is my full code so you can see what I am trying to do...
https://gist.github.com/jcranny/9465715
You are using an object as your array key and not the term_id of your series object.
function node_modify_riders_array($rider_id)
{
$fields = get_field("rider_series_data", $rider_id);
foreach($fields as $field_key => $field_val)
{
$new_fields[$field_val["series"]->term_id] = $field_val;
//^^^^^^^^^ <--- add this
}
return $new_fields;
}

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.

Display the value from a returned object

I have a variable object which stores the value below.
Array
(
[0] => stdClass Object
(
[term_id] => 1
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 1
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 4
[object_id] => 39
[cat_ID] => 1
[category_count] => 4
[category_description] =>
[cat_name] => Uncategorized
[category_nicename] => uncategorized
[category_parent] => 0
)
)
I now want to display the slug from the list of values. How do i do so?
Assuming that object is stored in an array called $array:
echo $array[0]->slug
You can use print_r($yourObject) to print out the entire object for debugging or if you want to print just that value this would work: echo($yourObject[0]->slug); .
If there are multiple array indices then this:
foreach($yourObject as $object)
{
echo $object->slug;
}
Jonh gave you an exact answer...let me explain it a bit
$obj = your_array;//from where you are using var_dump() to see these values..
and then
echo $obj->slug
you can use the same technique for other terms in your dump, like
echo $obj->name
HTH

Categories