Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The following code is adapted from Jan Fabry's answer. Mixing custom post type and taxonomy rewrite structures?

<?php
add_filter('post_type_link', 'client_area_permalink', 10, 4);
function client_area_permalink($post_link, $post, $leavename, $sample) {
    if ( false !== strpos( $post_link, '%client_category%' ) ) {
        $glossary_letter = get_the_terms( $post->ID, 'client_category' );
        $post_link = str_replace( '%client_category%', array_pop( $glossary_letter )->slug, $post_link );
    }
    return $post_link;
}
?>

When I go to add a new post, array_pop throws an error. I believe it is because there is no taxonomy associated yet with the post. array_pop error

How do I go about fixing this error? I have thought about using get_post_permalink as a base because that is where post_type_link is hooked into.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

In order to avoid an error like this you'll have to check whether or not the value of $glossary_letter is an array or not. Since get_the_terms() returns false on failure, you can just check whether or not $glossary_letter is false and set it to an empty array in order to appease array_pop() without getting that error.

<?php
add_filter('post_type_link', 'client_area_permalink', 10, 4);
function client_area_permalink($post_link, $post, $leavename, $sample) {
    if ( false !== strpos( $post_link, '%client_category%' ) ) {
        $glossary_letter = get_the_terms( $post->ID, 'client_category' );

        // adding check and reset
        if ( !$glossary_letter ) { $glossary_letter = array(); }
        // continue as you were

        $post_link = str_replace( '%client_category%', array_pop( $glossary_letter )->slug, $post_link );
    }
return $post_link;
}
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.