0

I have two custom Taxonomies in wordpress which have posts attached to them, like the following example:

Taxanomy A
Taxanomy B

Post 1
Post 2

Each post have the two taxonomies attached to it depends the need. I need to output that for every Taxonomy A there is Taxanomy B and for every Taxonomy B there X number of posts.

I want to achieve this in arrays to eventually output then in a JSON format and this is what I managed to write up and working however, the results are not correct:

$TaxanomiesA = get_terms("TaxanomyA", array('hide_empty' => false));
$TaxanomiesB = get_terms("TaxanomyB", array('hide_empty' => false));

$TaxanomyAArray = array();
$TaxanomyBArray = array();
$postssArray = array();

foreach($TaxanomiesA as $TaxanomyA) {
        $TaxanomyAArray[] = array (  
        'id' => $TaxanomyA->term_id,
        'name' => $TaxanomyA->name,
        'TaxanomyB' => $TaxanomyBArray
    );

    $args = array(//Arguments);
    $posts = get_posts( $args );

        foreach($TaxanomiesB as $TaxanomyB){
                $TaxanomyBArray[] = array (  
                        'id' => $TaxanomyB->term_id,
                        'name' => $TaxanomyB->name,
                        'posts' => $postssArray
                    );
            foreach($posts as $post){
                    $postssArray[] = array (  
                        'id' => $post->ID,
                        'name' => $post->title
                    );
            }

        }

    }  

    $jsonOutput=array(
        'JSON' => $TaxanomyAArray
    );

    echo json_encode($jsonOutput, JSON_UNESCAPED_SLASHES);                                            

}

This is the output I want to achive:

{
   "JSON":[
      {
         "id":"1",
         "name":"TaxonomyA Name",
         "Taxonomy":[
            {
               "id":"1",
               "name":"TaxonomyB Name",
               "posts":[
                  {
                     "id":"1",
                     "name":"Post Title"
                  },
                  {
                     "id":"2",
                     "name":"Post Title 2"
                  }
               ]
            }
         ]
      }
   ]
}
  • AFAIK taxonomies cannot be related to each other, besides parent-child relationships in a single hierachical taxonomy. Besides that, can you give an example of the data that you have, and an example of the output you want? (with both positive examples: post in taxonomy term 1 is listed - and negative: post in taxonomy term 2 is not listed) – Jen Nov 22 '16 at 20:54
  • @guiniveretoo - I have updated the post with the expected output I want to achieve. My problem Is I am nearly there but posts are getting repeated due to the array being in the foreach loop – Vladros Nov 22 '16 at 21:03
  • So, to clarify, for each top-level term (A), you want to loop through the child terms (B). For each of those child terms, you want to loop through posts categorized in that term. Does that sound correct? – Jen Nov 22 '16 at 21:09
  • Is it possible to need more than two levels of taxonomy terms? As in, A->B-->C? – Jen Nov 22 '16 at 21:10
  • @guiniveretoo - no more than two level of taxonomies, so yeas as you said I need to get all the posts who have taxonomy B and taxonomy A – Vladros Nov 22 '16 at 21:11
1

The issue is that the get_posts function is being called outside of the $TaxanomiesB loop. Move it inside and make sure the current $TaxanomyB is being used in the $args in get_posts.

Add data to $TaxanomyAArray and $TaxanomyBArray at the end of each for-each loop, otherwise the first object in the array will just be empty.

You also need to re-initialze the $array variables at the start of each parent loop, otherwise you'll end up with repeats of each post/taxonomy multiple times per set (like 'a' => (1 => (foo, bar), 2 => (foo, bar, foo, bar)), 'b' => 1 => (foo, bar, foo, bar, foo, bar) ...etc).

$TaxanomiesA = get_terms("TaxanomyA", array('hide_empty' => false));
$TaxanomiesB = get_terms("TaxanomyB", array('hide_empty' => false));

$TaxanomyAArray = array();

foreach($TaxanomiesA as $TaxanomyA) {
    $TaxanomyBArray = array();
    foreach($TaxanomiesB as $TaxanomyB){
        $args = array(//Arguments with $TaxonomyB);
        $posts = get_posts( $args );
        $postsArray = array();
        foreach($posts as $post){
            $postsArray[] = array (  
                'id' => $post->ID,
                 'name' => $post->title
            );
        }
        $TaxanomyBArray[] = array (  
            'id' => $TaxanomyB->term_id,
            'name' => $TaxanomyB->name,
            'posts' => $postsArray
        );
    }

    $TaxanomyAArray[] = array (  
        'id' => $TaxanomyA->term_id,
        'name' => $TaxanomyA->name,
        'TaxanomyB' => $TaxanomyBArray
    );
}  

$jsonOutput = array(
     'JSON' => $TaxanomyAArray
);

echo json_encode($jsonOutput, JSON_UNESCAPED_SLASHES);                                            

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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