WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
Problems with custom post type archive titles (8 posts)

  1. AndyDeGroo
    Member
    Posted 2 years ago #

    I'm having some problems with custom post type archive title.
    In template functions.php I have registered custom post type 'ily_events' with archive slug 'events'. There are two languages defined (Latvian and English) with few event posts in each language published.

    polylang settings dump:

    Array
    (
        [browser] => 1
        [rewrite] => 1
        [hide_default] => 0
        [force_lang] => 1
        [version] => 0.7.2
        [default_lang] => lv
    )

    opening url /en/events/ or /lv/events/
    gives two PHP notices:

    Undefined property: stdClass::$labels
    includes/general-template.php on line 665

    and
    Trying to get property of non-object in [...]/wp-includes/general-template.php on line 665

    It throws out in wp_title() and after debugging I found that incorrect object is returned from get_queried_object(); in post_type_archive_title() in /wp-includes/general-template.php on line 664.
    doing var_dump($post_type_obj); on next line shows corresponding language object:

    object(stdClass)[120]
      public 'term_id' => string '6' (length=1)
      public 'name' => string 'English' (length=7)
      public 'slug' => string 'en' (length=2)
      public 'term_group' => string '1' (length=1)
      public 'term_taxonomy_id' => string '6' (length=1)
      public 'taxonomy' => string 'language' (length=8)
      public 'description' => string 'en_US' (length=5)
      public 'parent' => string '0' (length=1)
      public 'count' => string '5' (length=1)

    but instead it should return a post object. Other archive titles are generated correctly.
    Maybe polylang is missing some filters required to return correct object or it is messing up correct values.

    Also while debugging I got these notices on saving post with tags:

    PHP Notice: Trying to get property of non-object in [...]/wp-content/plugins/polylang/include/base.php on line 133
    PHP Notice:  Undefined variable: newterms in [...]/wp-content/plugins/polylang/include/admin-filters.php on line 384

    don't know about first yet but second one is trivial - unset variable.
    corrected the latter by adding $newterms = array(); on line 380

    I'll try to debug further and post here.

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Thank you for this very detailed bug report.

    For the first issue, edit the file include/core.php at line 318 (0.7.2) and replace :

    if (isset($qvars['lang']) && $qvars['lang'] && is_author())

    by

    if (isset($qvars['lang']) && $qvars['lang'] && (is_author() || is_post_type_archive()))

    It should solve the issue.

    For the notices on the admin side, I do not understand why they are not displayed on my test site. I will make the correction you propose.

  3. AndyDeGroo
    Member
    Posted 2 years ago #

    Thanks for quick response. This solves the glitch.

    As for the notices - those happen only when saving posts with tags and with WP_DEBUG set to true.

    On development sites I usually define the debug switch based on session variable in wp-config.php which can be turned on and off with simple $_GET variable.

    here's the snippet from my wp-config.php:

    /**
     Define sessions name just in case
     and start session
     */
    session_name('WP_SESSION');
    if(session_id()==''){
    	session_start();
    }
    if(isset($_GET['debug']) && $_GET['debug']!='off'){
    		$_SESSION['WP_DEBUG'] = true;
    }elseif(isset($_GET['debug']) && $_GET['debug']=='off'){
    		unset($_SESSION['WP_DEBUG']);
    }
    
    /** Conditional debugging */
    if(!defined('WP_DEBUG')){
    	define('WP_DEBUG', isset($_SESSION['WP_DEBUG']) );
    	// save debug.log in wp-content/
    	define('WP_DEBUG_LOG', WP_DEBUG);
    }
    /** Show errors only if in debug mode */
    if(WP_DEBUG){
    	ini_set('display_errors','On');
    }else{
    	ini_set('display_errors','Off');
    }

    I'm monitoring wp-content/debug.log for new messages because some messages can be obscured if they happen in admin header for instance.

    Hope this will come in handy for you or others who read this thread.

  4. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Thank you for sharing. I always set WP_DEBUG to true, but I already noticed that notices were not displayed on the edit post panel (I guess it is due to the redirection made by WordPress and I wonder why some people do see these notices).

    However I didn't know about the WP_DEBUG_LOG option... And obviously it will be very helpful. Thank you !

  5. AndyDeGroo
    Member
    Posted 2 years ago #

    I'm glad you found my snippet useful.

    Pardon me, but there is another CPT related bug.
    Polylang_Core::get_translation_url returns incorrect CPT archive URL if rewrite slug is different from post_type.
    By default CPT archive slug is post type, but custom slug can be defined in register_post_type as rewrite['slug']='diferent-slug' or with has_archive option.

    I did a mod in include/core.php around line 499:

    if (is_post_type_archive())
    	$url = esc_url($base.$qvars['post_type'].'/');

    and replaced by:

    if (is_post_type_archive()){
    	$cpt_rewrite = get_post_type_object($qvars['post_type'])->rewrite;
    	$cpt_slug = (isset($cpt_rewrite['slug']))? $cpt_rewrite['slug'] : $qvars['post_type'];
    	$url = esc_url($base.$cpt_slug.'/');
    }
  6. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    I was not aware of this. Thanks for sharing. I will make the correction.

  7. AndyDeGroo
    Member
    Posted 2 years ago #

    CPT archive titles now work correctly in Polylang 0.8.

    Unfortunately there is another CPT related bug;
    CPT archive links are incorrect if $cpt->has_archive is string and differs from CPT name or slug.
    Actually, you just need to get the slug from $cpt->has_archive if it is a string;

    This makes things simpler in core.php @ 518:

    $cpt = get_post_type_object($qvars['post_type']);
    // we don't need to check if there is $cpt->rewrite['slug'] because WP always sets it
    // see /wp-includes/post.php in register_post_type() on line 995
    $cpt_slug = is_string($cpt->has_archive) ? $cpt->has_archive : $cpt->rewrite['slug'];
    $url = esc_url($base.$cpt_slug.'/');

    And, I believe, there could be other underwater stones hiding in $cpt->hierarchical. Haven't seen any tho.
    I'm currently trying to create a structure of _different_ hierarchical post types and some CPT limitations can be seen already, but that's mostly unrelated to Polylang.

  8. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    So I still need to improve custom post type support ! Thank you. I will correct this for the next release.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags