I have a custom post type of 'artworks' and a custom taxonomy of 'artists'. On page-artists.php I have created a custom list of all the artists and would like to link each artist's name to the first post associated with that term instead of going to the term's page. This is my current code:
<?php while ( have_posts() ) : the_post(); ?>
<?php
$args = array(
'post_type' => 'artworks', // change this to the post type you registered
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'artists',
'field' => 'id'
)
)
);
$first_cat_post = get_posts($args);
$terms = get_terms('artists', $args);
// list artists alphabetically by last name
$count = count($terms); $i=0;
if ($count > 0) {
$term_list = '<ul class="artist-list">';
foreach ($terms as $term) {
$i++;
// group terms by first letter
$this_char = strtoupper(substr($term->name,0,1));
if ($this_char != $last_char) {
$last_char = $this_char;
$term_list .= '<li><h2>'.$last_char.'</h2></li>';
}
$termid = 'artists_' . ($term->term_id);
$termfirst = get_field('first_name', $termid);
// link artist name with first work by that artist
$term_list .= '<li><a href="' . get_term_link( $term->slug, $term->taxonomy ) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $termfirst . ' ' . $term->name . '</a></li>';
}
$term_list .= '</ul>';
echo $term_list;
}
?><?php endwhile; // end of the loop. ?>
Here is the dev site. Right now the term list items are linking to that terms page, but it is a 404. This Answer looks promising, but I do not know what template file to put the code in. I tried putting this code:
<?php
/*
Redirect To First Child
*/
if (have_posts()) {
while (have_posts()) {
the_post();
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
$firstchild = $pagekids[0];
wp_redirect(get_permalink($firstchild->ID));
}
}
?>
in taxonomy-artists.php, but nothing happens.