Support » Plugin: Polylang » Custom post types author archives and Polylang

  • Resolved miguelcortereal


    Author archives for regular posts works fine, it returns the list of posts by author and by language.

    By default WP doesn’t add custom post types to the query, it has to be made manually which I already did.

    add_action( 'pre_get_posts', 'add_cpt_to_query' );
    function add_cpt_to_query( $query ) {
    	if ( (is_home() && $query->is_main_query()) || ($query->is_author))
    		$query->set( 'post_type', array( 'post', 'my_cpt' ) );
    	return $query;

    When I list each author I get all posts and all CPT by language.
    For example:
    …./pt/author/admin/ returns posts and CPT from Admin in Portuguese,
    …./es/author/admin/ returns posts and CPT from Admin in Spanish.

    Displaying one author when there’s no regular posts but only CPTs, the translated pages link to homepage.

    Looks like that Polylang isn’t checking for custom post types before returning the translated links or the homepage.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Chouby


    The issue should not be custom post types by themselves but the fact that Polylang does not expect two post types in a main query… Do you log errors in debug mode?

    I’ve just set debug mode, there are several notices and warnings but nothing related with Polylang.

    Do you think that I should not couple ‘post’ and ‘CPT’ on the main query but instead target specific circunstances to display CPTs?

    For example:

    given the fact that my members will be only posting CPTs and posts will be only me (admin) posting, instead of the previous function maybe this one:

    add_action( 'pre_get_posts', 'add_cpt_to_query' );
    function add_cpt_to_query( $query ) {
    	if ( ($query->is_author) && !is_author('admin') )
    		$query->set( 'post_type', array( 'my_cpt' ) );
    	return $query;

    And by the way, the only thing missing right now is to have each flag from the language switcher linking to the correspondent translated archive author page instead of the homepage.
    This only happens when an author posts listing is displayed and this author doesn’t have regular posts associated.
    Other than that works fine.

    Plugin Author Chouby


    Polylang normally checks that the correspondent translated archive page has some content. If the page is empty, it links to the home page to avoid any 404 error. Has your page some content that, in this case, would not be detected by Polylang?

    The link displays all Portuguese posts from User A.

    The link displays all Spanish posts from User A.

    Being in any of those pages, the flags link to homepage.

    This happens only for users that only have Custom post types and none regular post.

    If the user have both post types the flags work correctly.

    As you said Polylang looks for existing content to determine to link for the correspondent language archives page or to homepage.
    The definition of content right now is only posts, what I’m suggesting is to include also post types.

    Apart from the suggestion which I understand if you don’t consider as an improvement, once that WP by default also doesn’t, ideally the code to broaden polylang query so that it starts including my custom post type at least for author archives.

    Plugin Author Chouby


    I initially created a function inspired by wp_count_posts. Since the main query has (generally) only one post type and since this function also supports only one post type, I did not think I would need to support several. But I see no reason why it would be an impossible improvement 🙂

    Waiting for this improvement, you maybe code something for you with the pll_translation_url filter.

    Plugin Author Chouby


    Could you check if current development version ( does fit your needs?

    Not yet, country flags still link to homepage when a user doesn’t have regular posts, but only custom posts type.

    Plugin Author Chouby


    You will need 2 actions. First update to the current development version (

    Then add this code snippet in a custom plugin or in your functions.php

    add_filter('pll_translation_url', 'author_translation_url', 10, 2);
    function author_translation_url($url, $lang) {
    	if (empty($url) && is_author()) {
    		global $polylang, $wp_query;
    		$language = $polylang->model->get_language($lang);
    		if ($polylang->model->count_posts($language, $wp_query->query_vars))
    			$url = $polylang->links->get_archive_url($language);
    	return $url;

    Maybe I will add this support of modified query vars in the future but this needs further tests so I am not ready yet to include this in Polylang.

    Thanks a lot Chouby!

    That snippet of code works fine to me. I’ve been trying to code something as you suggested but only got it working partially.

    Now each country flag respond correctly linking to homepage or to author page by language whether the author has content to show or not with custom post types included.

    I can confirm too that there was a similiar issue at blog page, when homepage is set as a static page, country flags at the blog page (page of posts) were not linking to translation page but to homepage.
    With it got fixed.

    If you decide to not include in Polylang modified query vars support, my opinion is to include at least a tutorial on documentation about how to get Polylang handling it.

    Marking this topic as resolved.

    Hi there,

    Is there any news on the modified query vars front?

    Polylang currently prevents my custom post types from showing up on my ‘Latest’ page using the following custom query:

        'post_type' => array( 'magazine', 'presse', 'post' ),
        'post_status' => 'publish',
        'posts_per_page' => 8,
        'orderby' => 'post_date',
        'order' => 'DESC'
    $query = new WP_Query($args);

    Disabling Polylang gets the posts to appear correctly. Is there any way to get this to work?

    Thanks in advance…


    Hi there,

    the snippet suggested by Chouby doesn’t work for me. Instead, i’ve used this one:

    I’ve also had to filter the author’s archive query to get posts from all custom post types.

    I hope this helps someone.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Custom post types author archives and Polylang’ is closed to new replies.