1

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() {

 $uid = get_current_user_id();
 $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
 $customer_account  = get_field('customer_account', 'user_'.$uid );



   $args = array(
     'post_type' => 'cpt',
     'posts_per_page' => -1,
       'orderby' => 'date',
     'tax_query' => array(
       'relation' => 'AND',
       array(
         'taxonomy' => 'customer',
         'field'    => 'term_id',
         'terms'    => $customer_account,
       )
     )
   );

       $the_query = new WP_Query($args);
       $results = array();


               if (!empty($the_query->posts))
               {
                   foreach ($the_query->posts as $post)
                   {
                       $id = $post->ID;
                       array_push($results, array(
                           'id' => $id,
                           'title' => get_field('e_detail_title', $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
)

                )

        )
4
  • I can't tell what you're trying to do. Your ajax-filter.php code doesn't make a lot of sense. You're looping through $the_query, which hasn't been made in your function or retrieved from anywhere. Then you're pushing values into an array, encoding it into JSON, immediately decoding it, then looping through and echoing a property that doesn't exist. Not to mention the values you're putting in are term objects into a tax field. Also filter() is a dangerously generic name for a function (and doesn't appear to describe what you're doing). What are you actually trying to accomplish? Sep 12, 2017 at 11:12
  • @JacobPeattie i updated the code. i forgot to place that line in.
    – Dionoh
    Sep 12, 2017 at 11:20
  • That's great but you're still going to have to explain what you're trying to do. Sep 12, 2017 at 11:23
  • @JacobPeattie When i use my data as $value->title; i get my title. but what i want is from within my tax array to show the id of my tax. it would be something like this $value->tax->term_id;
    – Dionoh
    Sep 12, 2017 at 11:33

1 Answer 1

1

You are trying to access an object inside an array. To retrieve the data from term_id, you can use:

$value[0]['tax'][0]->term_id

Notice that the first 3 steps are arrays, and then there is a WP_Object, which has an array inside.

Breaking Down the Code

$value                          - returns the entire array
$value[0]                       - returns the first element of the array
$value[0]['tax']                - returns the `tax` element and all of its contents
$value[0]['tax'][0]             - returns the first element of `tax`
$value[0]['tax'][0]->term_id    - points to the term id inside the above

UPDATE

You don't need to use json_encode or json_decode. Simply use a foreach to output the content:

foreach( $results as $key => $value) {
    echo $value['tax']->term_id;
}

You might also want to use $value['tax'][0]->term_id if there are multiple terms inside the tax.

7
  • When i put this code in, nothing happens.
    – Dionoh
    Sep 12, 2017 at 12:13
  • echo $value[0]['tax'][0]->term_id;
    – Dionoh
    Sep 12, 2017 at 12:13
  • @Dionoh Does $value[0]['tax'][0]['term_id'] work? When you encode and decode from json (why are you doing this) you're turning everything into an array. Sep 12, 2017 at 12:18
  • Not working. What would you do instead of encode and decode?
    – Dionoh
    Sep 12, 2017 at 12:19
  • I used echo '<pre>'; print_r($value); echo '</pre>';
    – Dionoh
    Sep 12, 2017 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.