src/class-extended-cpt.php
public function post_type_link( string $post_link, WP_Post $post, bool $leavename, bool $sample ) : string {

		# If it's not our post type, bail out:
		if ( $this->post_type !== $post->post_type ) {
			return $post_link;
		}

		$date = explode( ' ', mysql2date( 'Y m d H i s', $post->post_date ) );
		$replacements = [
			'%year%'     => $date[0],
			'%monthnum%' => $date[1],
			'%day%'      => $date[2],
			'%hour%'     => $date[3],
			'%minute%'   => $date[4],
			'%second%'   => $date[5],
			'%post_id%'  => $post->ID,
		];

		if ( false !== strpos( $post_link, '%author%' ) ) {
			$replacements['%author%'] = get_userdata( $post->post_author )->user_nicename;
		}

		foreach ( get_object_taxonomies( $post ) as $tax ) {
			if ( false === strpos( $post_link, "%{$tax}%" ) ) {
				continue;
			}

			$terms = get_the_terms( $post, $tax );
			if ( $terms ) {

				/**
				 * Filter the term that gets used in the `$tax` permalink token.
				 * @TODO make this more betterer ^
				 *
				 * @param WP_Term   $term  The `$tax` term to use in the permalink.
				 * @param WP_Term[] $terms Array of all `$tax` terms associated with the post.
				 * @param WP_Post   $post  The post in question.
				 */
				$term_object = apply_filters( "post_link_{$tax}", reset( $terms ), $terms, $post );

				$term = get_term( $term_object, $tax )->slug;

			} else {
				$term = $post->post_type;

				/**
				 * Filter the default term name that gets used in the `$tax` permalink token.
				 * @TODO make this more betterer ^
				 *
				 * @param string  $term The `$tax` term name to use in the permalink.
				 * @param WP_Post $post The post in question.
				 */
				$default_term_name = apply_filters( "default_{$tax}", get_option( "default_{$tax}", '' ), $post );
				if ( $default_term_name ) {
					$default_term = get_term( $default_term_name, $tax );
					if ( ! is_wp_error( $default_term ) ) {
						$term = $default_term->slug;
					}
				}
			}

			$replacements[ "%{$tax}%" ] = $term;

		}

		$post_link = str_replace( array_keys( $replacements ), $replacements, $post_link );

		return $post_link;

	}
public function post_type_link( string $post_link, WP_Post $post, bool $leavename, bool $sample ) : string {

		# If it's not our post type, bail out:
		if ( $this->post_type !== $post->post_type ) {
			return $post_link;
		}

		$date = explode( ' ', mysql2date( 'Y m d H i s', $post->post_date ) );
		$replacements = [
			'%year%'     => $date[0],
			'%monthnum%' => $date[1],
			'%day%'      => $date[2],
			'%hour%'     => $date[3],
			'%minute%'   => $date[4],
			'%second%'   => $date[5],
			'%post_id%'  => $post->ID,
		];

		if ( false !== strpos( $post_link, '%author%' ) ) {
			$replacements['%author%'] = get_userdata( $post->post_author )->user_nicename;
		}

		foreach ( get_object_taxonomies( $post ) as $tax ) {
			if ( false === strpos( $post_link, "%{$tax}%" ) ) {
				continue;
			}

			$terms = get_the_terms( $post, $tax );
			if ( $terms ) {

				/**
				 * Filter the term that gets used in the `$tax` permalink token.
				 * @TODO make this more betterer ^
				 *
				 * @param WP_Term   $term  The `$tax` term to use in the permalink.
				 * @param WP_Term[] $terms Array of all `$tax` terms associated with the post.
				 * @param WP_Post   $post  The post in question.
				 */
				$term_object = apply_filters( "post_link_{$tax}", reset( $terms ), $terms, $post );

				$term = get_term( $term_object, $tax )->slug;

			} else {
				$term = $post->post_type;

				/**
				 * Filter the default term name that gets used in the `$tax` permalink token.
				 * @TODO make this more betterer ^
				 *
				 * @param string  $term The `$tax` term name to use in the permalink.
				 * @param WP_Post $post The post in question.
				 */
				$default_term_name = apply_filters( "default_{$tax}", get_option( "default_{$tax}", '' ), $post );
				if ( $default_term_name ) {
					$default_term = get_term( $default_term_name, $tax );
					if ( ! is_wp_error( $default_term ) ) {
						$term = $default_term->slug;
					}
				}
			}

			$replacements[ "%{$tax}%" ] = $term;

		}

		$post_link = str_replace( array_keys( $replacements ), $replacements, $post_link );

		return $post_link;

	}
public function post_type_link ( string $ post_link , WP_Post $ post , bool $ leavename , bool $ sample ) : string { # If it's not our post type, bail out: if ( $ this -> post_type !== $ post -> post_type ) { return $ post_link ; } $ date = explode ( ' ' , mysql2date ( 'Y m d H i s' , $ post -> post_date ) ) ; $ replacements = [ '%year%' => $ date [ 0 ] , '%monthnum%' => $ date [ 1 ] , '%day%' => $ date [ 2 ] , '%hour%' => $ date [ 3 ] , '%minute%' => $ date [ 4 ] , '%second%' => $ date [ 5 ] , '%post_id%' => $ post -> ID , ] ; if ( false !== strpos ( $ post_link , '%author%' ) ) { $ replacements [ '%author%' ] = get_userdata ( $ post -> post_author ) -> user_nicename ; } foreach ( get_object_taxonomies ( $ post ) as $ tax ) { if ( false === strpos ( $ post_link , "%{$tax}%" ) ) { continue ; } $ terms = get_the_terms ( $ post , $ tax ) ; if ( $ terms ) { /**
				 * Filter the term that gets used in the `$tax` permalink token.
				 * @TODO make this more betterer ^
				 *
				 * @param WP_Term   $term  The `$tax` term to use in the permalink.
				 * @param WP_Term[] $terms Array of all `$tax` terms associated with the post.
				 * @param WP_Post   $post  The post in question.
				 */ $ term_object = apply_filters ( "post_link_{$tax}" , reset ( $ terms ) , $ terms , $ post ) ; $ term = get_term ( $ term_object , $ tax ) -> slug ; } else { $ term = $ post -> post_type ; /**
				 * Filter the default term name that gets used in the `$tax` permalink token.
				 * @TODO make this more betterer ^
				 *
				 * @param string  $term The `$tax` term name to use in the permalink.
				 * @param WP_Post $post The post in question.
				 */ $ default_term_name = apply_filters ( "default_{$tax}" , get_option ( "default_{$tax}" , '' ) , $ post ) ; if ( $ default_term_name ) { $ default_term = get_term ( $ default_term_name , $ tax ) ; if ( ! is_wp_error ( $ default_term ) ) { $ term = $ default_term -> slug ; } } } $ replacements [ "%{$tax}%" ] = $ term ; } $ post_link = str_replace ( array_keys ( $ replacements ) , $ replacements , $ post_link ) ; return $ post_link ; }
Filter the post type permalink in order to populate its rewrite tags.

@param  string  $post_link The post's permalink.
@param  WP_Post $post      The post in question.
@param  bool    $leavename Whether to keep the post name.
@param  bool    $sample    Is it a sample permalink.
@return string             The post's permalink.
Filter the post type permalink in order to populate its rewrite tags .