I am building a wordpress website that uses a custom AJAX filter that is linked to my Custom categories.
This is my function ajax-filter.php:
function filter() {
$results = array();
if (!empty($the_query->posts))
{
foreach ($the_query->posts as $post)
{
$id = $post->ID;
array_push($results, array(
'id' => $id,
'tax' => get_the_terms( $post->ID, 'type', $id ),
));
}
}
wp_reset_postdata();
$someJSON = json_encode($results);
// Convert JSON string to Object
$someObject = json_decode($someJSON);
foreach($someObject as $key => $value) {
echo $value->title;
}
die;
}
add_action( 'wp_ajax_filter', 'filter' );
add_action( 'wp_ajax_nopriv_filter', 'filter' );
My question is:
How can i use my data from tax to term-id in the way that i use in my ajax-filter.php? to use my data ($value->title)
Array
(
[0] => Array
[title] => title
[tax] => Array
(
[0] => WP_Term Object
(
[term_id] => 54
)
)
)
Advertisement
Answer
Firstly, well done on creating a Minimal example of your code, it makes it easier for us to see the problem and how to help 🙂
Now to your question:
There are 2 issues before you even get to your actual question
get_the_terms
only takes 2 parameters – you don’t need to pass ni the 3rd parameter $id. The id is passed in the 1st parameter alreadyget_the_terms
returns an array because a post can have multiple terms associated with it. You will need to consider how to handle multiple terms.
Handle a single term per post
For now, I’m going to assume you are only going to have a single term associated with the post, or if there are more only return the first.
Update your code as follows – the explanation for what is doing is in the comments:
if (!empty($the_query->posts)){
foreach ($the_query->posts as $post){
// add this before you push the the values to your $results array
// get an array with the term_ids only
$term_ids = array();
$term_objs = get_the_terms( $post->ID, 'type' );
// get_the_terms returns an array of WP_Term objects
foreach ($term_objs as $term_obj)
$term_ids[] = $term_obj->term_id; // get the id from the WP_Term object
// $term_ids could have many ids, so for the purpose of your example
// we're just going to get the first one
// You will need to decide how you'll handle multiple ids
if ($term_ids) $term_id = $term_ids[0];
$id = $post->ID;
array_push($results, array(
'id' => $id,
[// your other values here ]
'tax' => $term_id, // add just the single term id value
));
}
}
This will all it to your $results
array as follows:
Array
(
[0] => Array
(
[title] => title
[tax] => 54
)
)
Now you can access the term id in your loop like this $value->tax
, e.g.
foreach($someObject as $key => $value) {
echo $value->title;
echo $value->tax; // or whatever you want to do with it....
}
Handle Multiple terms per post
If you wanted to handle multiple terms, you could push the $term_ids
array:
foreach ($term_objs as $term_obj)
$term_ids[] = $term_obj->term_id;
array_push($results, array(
'id' => $id,
[// your other values here ]
'tax' => $term_ids, // add the array of term ids
));
… and then loop through the array when you retrieve them:
foreach($someObject as $key => $value) {
$term_ids = $value->tax;
foreach ($term_ids as $term_id)
echo $term_id; // or whatever you want to do with it....
}