WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Custom post types author archives and Polylang (15 posts)

  1. miguelcortereal
    Member
    Posted 5 months ago #

    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.

    https://wordpress.org/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 5 months ago #

    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?

  3. miguelcortereal
    Member
    Posted 5 months ago #

    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?

  4. miguelcortereal
    Member
    Posted 5 months ago #

    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;
    }
  5. miguelcortereal
    Member
    Posted 5 months ago #

    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.

  6. Chouby
    Member
    Plugin Author

    Posted 5 months ago #

    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?

  7. miguelcortereal
    Member
    Posted 5 months ago #

    The link mysite.com/pt/author/user-A displays all Portuguese posts from User A.

    The link mysite.com/es/author/user-A 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.

  8. miguelcortereal
    Member
    Posted 5 months ago #

    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.

  9. Chouby
    Member
    Plugin Author

    Posted 5 months ago #

    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.

  10. Chouby
    Member
    Plugin Author

    Posted 5 months ago #

    Could you check if current development version (1.4.1.2) does fit your needs?
    http://downloads.wordpress.org/plugin/polylang.zip

  11. miguelcortereal
    Member
    Posted 5 months ago #

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

  12. Chouby
    Member
    Plugin Author

    Posted 5 months ago #

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

    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.

  13. miguelcortereal
    Member
    Posted 5 months ago #

    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 1.4.1.7 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.

  14. miguelcortereal
    Member
    Posted 5 months ago #

    Marking this topic as resolved.

  15. sbckmstr
    Member
    Posted 4 months ago #

    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:

    $args=array(
        '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ā€¦

    Simon

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.