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

    https://www.ads-software.com/extend/plugins/polylang/

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

    (@chouby)

    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.

    Thread Starter Andis

    (@andydegroo)

    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.

    Plugin Author Chouby

    (@chouby)

    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 !

    Thread Starter Andis

    (@andydegroo)

    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.'/');
    }

    Plugin Author Chouby

    (@chouby)

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

    Thread Starter Andis

    (@andydegroo)

    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.

    Plugin Author Chouby

    (@chouby)

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Plugin: Polylang] Problems with custom post type archive titles’ is closed to new replies.