1

I am trying to filter posts by author. The 'post_author' is a custom taxonomy and I've included it in my args and posts are showing with the linked author names but when I click on the author name link, I get all authors. How can I show only the author that is clicked? I've included the relevant pieces of code:

//ADD THE TAXONOMY 'post_authors' TO MY CUSTOM POST TYPE
function add_custom_taxonomies() {
   register_taxonomy('post_authors', 'my-post-type', array(
   'hierarchical' => true,
   'labels' => array(
   'name' => _x( 'Authors', 'taxonomy general name' ),
   'singular_name' => _x( 'Author', 'taxonomy singular name' ),
   'search_items' =>  __( 'Search Authors' ),
   'all_items' => __( 'All Authors' ),
   'parent_item' => __( 'Parent Author' ),
   'parent_item_colon' => __( 'Parent Author:' ),
   'edit_item' => __( 'Edit Author' ),
   'update_item' => __( 'Update Author' ),
   'add_new_item' => __( 'Add New Author' ),
   'new_item_name' => __( 'New Author Name' ),
   'menu_name' => __( 'Authors' ),
   ),
   // Control the slugs used for this taxonomy
   'rewrite' => array(
   'slug' => 'post-authors', // This controls the base slug that will display before each term
   'with_front' => false, // Don't display the category base before "/authors/"
   'hierarchical' => true // This will allow URL's like "/authors/lorem/ipsum/"
 ),
));
}
add_action( 'init', 'add_custom_taxonomies', 0 );


// and in my archive page......

//ARGS
$args = array( 
    'post_type' => 'my-post-type',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_authors',
            'field'    => 'slug',
            'terms'    => 'Jill Jones',
        ),
    ),
);



//RETRIEVES AUTHOR NAME FROM CUSTOM TAXONOMY
$terms = get_the_terms( $post->ID, 'post_authors' );

if ($terms) {
    $terms_name = array();
    foreach ( $terms as $term ) {
    // The $term is an object, so we don't need to specify the $taxonomy.
    $term_link = get_term_link( $term );
    $terms_name[] = $term->name;
    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
      continue;
    }
}
$author_name = $terms_name[0]; 
}



//RETRIEVING THE AUTHOR NAME IN LOOP
if ( $post_typed == ('my-post-type') && $author_name  != '' )  {  echo '<a href="' . esc_url( $term_link ) . '    ">' . $author_name . '</a>'; } 
2

This is expected output from what you have in your code. Never change the main query for a custom query on any archive page or on the home page. Custom queries are always troublesome as the main query is quite specific on these pages.

I would advice you to rather use pre_get_posts to alter the main query on that specific archive page or home page you want to target, and leave the default loop in place.

Also, your tax query is completely wrong. It should be an array of an array. Please have a look at constructing a tax query in WP_Query

For further reading and a better understanding, please see my answer I recently did on a similar type of question

Sample code (place in functions.php and return to default loop in taxonomy.php):

function custom_author_page( $query ) {
    if ( !is_admin() && $query->is_tax( 'post_authors' ) && $query->is_main_query() ) {

        $query->set( 'post_type', 'my-post-type' );
        $query->set( 'order', 'DESC' );

    }
}
add_action( 'pre_get_posts', 'custom_author_page' );

EDIT

From your comments, you have named your taxonomy template taxonomy-post-author.php which relates to taxonomy post and term author. Rename your taxonomy template to taxonomy-post_authors.php

EDIT 2

OPTION 1

Change your taxonomy page to just the default loop, no custom loop.

<?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        //
        // Post Content here
        //
    } // end while
} // end if
?>

Change the main query with pre_get_posts as above. This is the correct method

OPTION 2

Get the queried term using get_queried_object('term');, and from that return the slug of the term. PS! this code should be used in taxonomy pages

$queried_object = get_queried_object('term');
$term_slug = $queried_object->slug;

Return $term_slug to your tax_query as terms

EDIT 3

To get an a array of term names/slugs/ID's from a specific taxonomy, use get_terms().

$terms = get_terms('post_authors');

$term_names = array();

if ( !empty( $terms ) && !is_wp_error( $terms ) ) { 

    foreach ( $terms as $key=>$term){
        $term_names[$key] = $term->name;
    }
}

You can then just simply pass $term_names to terms,

'terms'    => $term_names

EDIT 4

Use

<pre><?php var_dump($term_names); ?></pre>

to print the array. Output will look like this

array(6) {
  [0]=>
  string(12) "Admin se pen"
  [1]=>
  string(11) "Besprekings"
  [5]=>
  string(12) "Toets Parent"
  [6]=>
  string(15) "Uit die grapkas"
  [7]=>
  string(14) "Uit die koskas"
  [8]=>
  string(11) "Uit my lewe"
}
2
  • Don't understand "$term->name for the post_id" Aug 14 '14 at 19:14
  • Adding --- 'terms' => $term_names --- to my args was what I needed. Thank you again Pieter for all your help.
    – amespower
    Aug 14 '14 at 20:11

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.