1

I have a site where a CPT needs to have permalink structure with custom taxonomies (parent and child). Following this solution https://wordpress.stackexchange.com/a/75172/210080 I got custom permalinks with taxonomies working, but now I'm getting these PHP warnings:

NOTICE: PHP message: PHP Warning: Cannot use a scalar value as an array in /wp-content/themes/theme/functions.php on line 123"

"NOTICE: PHP message: PHP Warning: in_array() expects parameter 2 to be array, bool given in /wp-content/themes/theme/functions.php on line 124"

Those lines are:

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
    $visited[] = $parent -> parent;  

Full code is:

    add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules['products/(.+)/(.+)/?$'] = 'index.php?products=$matches[2]';
$newRules['products/(.+)/?$']                = 'index.php?pterm=$matches[1]'; 

return array_merge($newRules, $rules);
}

function filter_post_type_link($link, $post)
{
    if ($post->post_type != 'products')
        return $link;

    if ($terms = get_the_terms($post->ID, 'pterm'))
    {
        $link = str_replace('%pterm%', get_taxonomy_parents(array_pop($terms)->term_id, 'pterm', false, '/', true), $link); // see custom function defined below
    }
    return $link;

}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);



// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $nicename = false, $visited = array()) {  
    $chain = '';   
    $parent = &get_term($id, $taxonomy);

    if (is_wp_error($parent)) {
        return $parent;
    }

    if ($nicename)    
        $name = $parent -> slug;        
else    
        $name = $parent -> name;

    if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
        $visited[] = $parent -> parent;    
        $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $nicename, $visited);
        $chain .= "/";

    }

    if ($link) {
        // nothing, can't get this working :(
    } else    
        $chain .= $name;    
    return $chain;    
}

Hope someone can help!

2
  • Neither $parent -> parent, or $visited are arrays, it looks like the original author decided to just use $visited as an array by pulling it out of thin air and hoping PHP knew it was meant to be an array. that's why PHP gives a warning, because untill $visited[] happens it doesn't exist. Imagine if I started talking about the "boonboggle" but didn't explain what it was until afterwards, you'd be confused right? That's the same as PHP here, it's seeing $visited and saying "hmmm in_array needs an array but we got $visited, what on earth is $visited I've never seen it before
    – Tom J Nowell
    Commented Aug 11, 2021 at 16:37
  • Thank you @TomJNowell ! I was wondering about that part and seems that it wasn't neccessary and the code works without warnings after deleting that.
    – Hellosus
    Commented Aug 12, 2021 at 15:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.