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.
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.
Share Improve this question asked Jun 26, 2019 at 17:00 HeweHewe 859 bronze badges 1- Possible duplicate of Display all posts in a custom post type, grouped by a custom taxonomy – Camille V. Commented Jun 27, 2019 at 0:16
1 Answer
Reset to default 0You 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.