Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
// 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><li>' . the_terms() . '</li>';     

change the above lines to

$posts = get_posts(array(
    'post_type' => 'artworks',
    'numberposts' => 1,
    'post_status' => 'publish',
    'artists' => $term->slug,
    'orderby' => 'date',
    'order' => 'DESC'
));
// link artist name with first work by that artist
$term_list .= '<li><a href="' . get_permalink( $posts[0]->ID ) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $termfirst . ' ' . $term->name . '</a></li><li>' . the_terms() . '</li>';     

This will change the link to the latest post. You will probably want to modify the "orderby" & "order" parameters in the code. The possible values are available here http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

In your code you don't need the line $first_cat_post = get_posts($args);

Also please correct the parameters in the $args, see this page for reference http://codex.wordpress.org/Function_Reference/get_terms

share|improve this answer
    
Thank you! This is exactly what I was trying to do. I removed the follow parameter in the $args in the original question as it was not listed in codex. 'terms' => array($category->term_id) –  Bostow Nov 2 '12 at 21:35
add comment

I don't think get_pages is an appropriate use for this case, I believe get_posts is what you need. Also, wp_redirect won't work if its just thrown into your theme template file IF headers have already been sent. Its best executed from within your functions.php as in the example below.

We are hooking onto template_redirect which is the last action to fire before displaying presentation to the user (then comes get_header, etc...).

We then check that the query var is attempting to retrieve a term name which belongs to our taxonomy (in your case artists) and if that condition returns true we grab the first post, its permalink, then redirect the user to that first post.

Solution: (to be placed in your functions.php file)

add_action('template_redirect', 'first_post');

function first_post(){
    global $wp_query;
    global $post;

    if ( $wp_query->query_vars['artists'] === get_query_var('artists') ) {

    $args = array(
        'post_type' => 'artworks', // change this to the post type you registered
        'posts_per_page' => 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'artists',
                'field' => 'slug',
                'terms' => get_query_var( 'artists' )
                )
            )
    );

    $first_post = get_posts($args);
    foreach($first_post as $post) : setup_postdata($post); 
    wp_redirect(get_permalink($post->ID));
    exit;
    endforeach;

    }//end if-block conditional
}

The advantage of this method is that you get to keep your link structure appearance, example;

http://www.example.com/artists/michael-aakhus/
http://www.example.com/artists/jane-abrams/
etc...

Instead of your links appearing as,

http://www.example.com/artists/michael-aakhus/%postname%
http://www.example.com/artists/jane-abrams/%postname%
etc...

Until of course the user is redirected, then the address bar will show the appropriate URL with full post-name.

share|improve this answer
    
This solution also works, but I went with the answer that directly links the term list to the first post. –  Bostow Nov 2 '12 at 21:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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