$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Auto add custom taxonomy to permalink when save|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Auto add custom taxonomy to permalink when save

matteradmin9PV0评论

I've search this site and I can't find a way to automatically add custom taxonomy to a post permalink For example, I have a post with the title "mysamplepost" and taxonomy "years" with value "2018". So when I hit publish the post permalink becomes ""

I saw this post here but it doesn't function like I want to. Instead it inserts custom taxonomy before post title .

Thank you for whoever provides an answer

I've search this site and I can't find a way to automatically add custom taxonomy to a post permalink For example, I have a post with the title "mysamplepost" and taxonomy "years" with value "2018". So when I hit publish the post permalink becomes "https://example/mysamplepost-2018"

I saw this post here but it doesn't function like I want to. Instead it inserts custom taxonomy before post title .

Thank you for whoever provides an answer

Share Improve this question edited Dec 2, 2018 at 1:45 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Feb 14, 2018 at 5:54 juicebyahjuicebyah 1114 bronze badges 2
  • Do you want to append 2018 on the fly or want to append and store in database? – obiPlabon Commented Feb 14, 2018 at 7:04
  • append on database also so make it permanent man – juicebyah Commented Feb 14, 2018 at 10:55
Add a comment  | 

1 Answer 1

Reset to default 0

Please check the following code, you have to assign the custom taxonomy name to $taxonomy variable. And only the first taxonomy term will be appended to the post slug.

add_filter( 'wp_insert_post_data', function( $data, $postarr ) {
    /**
     * Only for post posttype and when the post is created
     */
    if ( empty( $postarr['save'] ) && isset( $data['post_type'] ) && 'post' === $data['post_type'] ) {
        $taxonomy  = 'custom-taxonomy-name'; // Add your custom taxonomy name
        $tax_terms = array();
        $slug      = '';

        /**
         * Make sure custom taxonomy is really assigned
         */
        if ( isset( $postarr['tax_input'] ) && isset( $postarr['tax_input'][ $taxonomy ] ) ) {
            $tax_terms = array_filter( $postarr['tax_input'][ $taxonomy ] );
        }

        /**
         * Only the first term slug will be appended
         */
        if ( count( $tax_terms ) ) {
            $term = get_term( current( $tax_terms ), $taxonomy );
            if ( ! is_wp_error( $term ) && ! is_null( $term ) ) {
                $slug = '-' . $term->slug;
            }
        }

        /**
         * Append the slug
         */
        if ( $slug ) {
            $data['post_name'] .= $slug;
        }
    }
    return $data;
}, 10, 2 );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far