Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Dennis

    (@dhoppe)

    You can change that WordPress behavior with the “pre_get_posts” filter to change the post type of the author archive.

    Thread Starter richard12

    (@richard12)

    Hi, Thanks for the reply. If anyone is interested add this to your theme functions.php and it will work a treat.

    function custom_post_author_archive($query) {
    if ($query->is_author)
    $query->set( ‘post_type’, array(‘encyclopedia’, ‘post’) );
    remove_action( ‘pre_get_posts’, ‘custom_post_author_archive’ );
    }
    add_action(‘pre_get_posts’, ‘custom_post_author_archive’);

    Thanks
    Richard

    Plugin Author Dennis

    (@dhoppe)

    Hi Richard, thanks for the snippet. I would submit two small improvements:

    1. The “is_author” method should be used instead of the public variable.
    2. To avoid incompatibilities with other plugins you should check if there are already other post types in the query var.
    Add_Action('pre_get_posts', function($query){
      If ($query->Is_Author()){
        If ($arr_post_type = $query->Get('post_type')){
          Array_Push($arr_post_type, 'encyclopedia');
          $query->Set('post_type', $arr_post_type);
        }
        Else {
          $query->Set('post_type', Array('post', 'encyclopedia'));
        }
      }
    });
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Author’ is closed to new replies.