0

I have a custom query for my taxonomy of:

    $terms = get_terms(
        array(
            'taxonomy' => "issueCompanion", 
            'hide_empty' => false, 
            'fields'=>'all'
        )
    )

which returns an array of publications (think along the lines of monthly magazines). I have an advanced custom field on the taxonomy by the name of published_year in which the user types a string of the year the magazine was published in.

In order to get the year on each taxonomy, I run this code within a for loop:

$year = get_field('published_year', $term)

I'm having trouble combining the two different requests into one loop without one thing or another showing up several times.

I need to create a single page in which I am showing the year, and then list all publications that match that year, like so:

2019
  Publication xxx
  Publication xxx
2018
  Publication xxx
  etc.

I would be most grateful for any input that helps me combine the two into one.

0

You can use a multidimensional array to get this format. Please see the example given below:

$terms = get_terms(
    array(
        'taxonomy' => "issueCompanion", 
        'hide_empty' => false, 
        'fields'=>'all'
    )
)

$multi_arr = array();

foreach($terms as $term){
    $year = get_field('published_year', $term->term_id);
    $multi_arr[$year][] = $term->name;
}

When you print this $multi_arr you will get the format you want. So you can manage your code according to this.

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.