php - How to get just the term_ids from get_the_terms in Wordpress -


solved

i found answer.

my problem (as many of told me) encoding , decoding stuff. i'm doing removed encode , decode , changed this:

array_push($results, array(                    'id' => $id,                    'tax' => get_the_terms( $post->id, 'type', $id ),                 )); 

to this:

array_push($results, (object) array(                        'id' => $id,                        'tax' => get_the_terms( $post->id, 'type', $id ),                     )); 

now able tax following:

foreach($value->tax $tax){                               echo  $tax->name . ', ' ;                      } 

without ajax screwing loop copying first taxonomy found , placing on every item.

thank helping.


problem solved above ^

i building wordpress website uses custom ajax filter linked custom categories.

this function ajax-filter.php:

function filter() {                $results = array();              if (!empty($the_query->posts))            {                foreach ($the_query->posts $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 object       $someobject = json_decode($somejson);        foreach($someobject $key => $value) {              echo $value->title;              }   die; }   add_action( 'wp_ajax_filter', 'filter' );   add_action( 'wp_ajax_nopriv_filter', 'filter' ); 

my question is:

how can use data tax term-id in way use in ajax-filter.php? use data ($value->title)

array (     [0] => array           [title] => title           [tax] => array                 (                     [0] => wp_term object                         (                             [term_id] => 54 )                  )          ) 

firstly, done on creating minimal example of code, makes easier see problem , how :)

now question:

there 2 issues before actual question

  1. get_the_terms takes 2 parameters - don't need pass ni 3rd parameter $id. id passed in 1st parameter already
  2. get_the_terms returns array because post can have multiple terms associated it. need consider how handle multiple terms.

handle single term per post

for now, i'm going assume going have single term associated post, or if there more return first.

update code follows - explanation doing in comments:

  if (!empty($the_query->posts)){        foreach ($the_query->posts $post){             // add before push the values $results array             // array term_ids            $term_ids = array();            $term_objs = get_the_terms( $post->id, 'type' );             // get_the_terms returns array of wp_term objects            foreach ($term_objs $term_obj)                $term_ids[] = $term_obj->term_id; // id wp_term object             // $term_ids have many ids, purpose of example            // we're going first 1            // need decide how you'll handle multiple ids            if ($term_ids) $term_id = $term_ids[0];              $id = $post->id;            array_push($results, array(                'id' => $id,                [...] // other values here                'tax' => $term_id, // add single term id value            ));        }    } 

this $results array follows: array ( [0] => array ( [title] => title [tax] => 54 ) )

now can access term id in loop $value->tax, e.g.

foreach($someobject $key => $value) {      echo $value->title;      echo $value->tax; // or whatever want it.... } 

handle multiple terms per post

if wanted handle multiple terms, push $term_ids array:

           foreach ($term_objs $term_obj)                $term_ids[] = $term_obj->term_id;              array_push($results, array(                'id' => $id,                [...] // other values here                'tax' => $term_ids, // add array of  term ids            )); 

... , loop through array when retrieve them:

foreach($someobject $key => $value) {     $term_ids = $value->tax;     foreach ($term_ids $term_id)          echo $term_id; // or whatever want it.... } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -