1

I am using the theme TwentyTwelve and modified the standard post query in the file author.php with the following code:

function wpd_author_query( $query ) {
//CODE to set $current_user_name here
//This gets the author from the URL
$author = get_user_by('slug',get_query_var('author_name'));
$current_user_name = $author->user_nicename;

    if ( $query->is_author() && $query->is_main_query()) {
            // your code to set $current_user_name here
            $query->set( 'meta_key', '_writer_relation_added_date_'.$current_user_name );
            $query->set( 'orderby', 'meta_value_num' );
            $query->set( 'post_status', $post_status );

            $tax_query = array(  
                array(
                    'taxonomy' => 'writer',
                    'field' => 'name',
                    'terms' => $current_user_name
                )
            );
            $query->set( 'tax_query', $tax_query );
    }               
}
add_action( 'pre_get_posts', 'wpd_author_query' );

However, the tax_query is not working for the author page. It is still not retrieving the posts in the writer taxonomy.

I did try applying that code for other pages such as is_home() is_archive() and it does work.

So, there must be a conflict in with wordpress and the author template page that is not allowing me to use the tax_query for retrieving posts in the writer taxonomy.

11
  • Your query mentions nothing about the specifying the current author, therefore your results don't either
    – Tom J Nowell
    Apr 19, 2014 at 23:10
  • See Milo's answer. You are wrong when say ".... replaced the standard post query in the file author.php..." You have not replaced the query, you have made a new one.
    – cybmeta
    Apr 19, 2014 at 23:50
  • @TomJNowell I did not post the code for retrieving the current author because it is not relevant.
    – Gixty
    Apr 20, 2014 at 0:09
  • @cybnet I did replace the code inside author.php with the one I posted.
    – Gixty
    Apr 20, 2014 at 0:14
  • 1
    @cybnet ok, sorry for not understanding. So my wording is wrong. I said "replaced" because I removed the existing code and put my own. Again, sorry for any misunderstanding.
    – Gixty
    Apr 20, 2014 at 20:52

1 Answer 1

4

Don't run a new query in the template, modify the main query before it's run via the pre_get_posts action in the theme's functions.php file.

function wpd_author_query( $query ) {
    if ( $query->is_author()
        && $query->is_main_query() ) {
            // your code to set $current_user_name here
            $query->set( 'meta_key', '_writer_relation_added_date_' . $current_user_name );
            $query->set( 'orderby', 'meta_value_num' );
            $tax_query = array(  
                array(
                    'taxonomy' => 'writer',
                    'field' => 'name',
                    'terms' => $current_user_name
                )
            )
            $query->set( 'tax_query', $tax_query );

            // EDIT
            // unset the requested author
            unset( $query->query_vars['author_name'] );
    }
}
add_action( 'pre_get_posts', 'wpd_author_query' );

You can then run the vanilla loop in the default author template, and save an extra query in the process.

5
  • I am still trying to get this to work. It is not retrieving the posts inside the writer taxonomy. I tried it in the home page with is_home() and it works, but it is not working in the author page.
    – Gixty
    Apr 20, 2014 at 0:15
  • it seems to work everywhere but in the author.php page. In the author page, it is retrieving only the posts made by the author and not querying the tax_query.
    – Gixty
    Apr 20, 2014 at 0:26
  • 1
    it's because the query has the author query var set based on the requested author. if you're using pretty permalinks, the query var is author_name. you can try to unset it so WordPress removes it from the query: unset( $query->query_vars['author_name'] );. see edit above.
    – Milo
    Apr 20, 2014 at 1:05
  • Well, that did the trick; however, now I need to set back the query var again after altering the main query. How would I do this?
    – Gixty
    Apr 20, 2014 at 1:44
  • Thanks Milo! I figured it out and edited my question to add the solution. I could not have done it without your help. Where can I mark the question as NOT DUPLICATE?
    – Gixty
    Apr 20, 2014 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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