AnsPress_PostTypes::post_type_link( string $link, object $post )

Description #

Alter question and answer CPT permalink.

Parameters #

  • $link
    string (Required) Link.
  • $post
    object (Required) Post object.

Changelog #

VersionDescription
2.0.0Introduced.

Source #

File: includes/post-types.php

	public static function post_type_link( $link, $post ) {
		if ( 'question' === $post->post_type ) {
			$question_slug = ap_opt( 'question_page_permalink' );

			if ( empty( $question_slug ) ) {
				$question_slug = 'question_perma_1';
			}

			$default_lang = '';

			// Support polylang permalink.
			if ( function_exists( 'pll_default_language' ) ) {
				$default_lang = pll_default_language();
			}

			if ( get_option( 'permalink_structure' ) ) {
				$structure = self::question_perm_structure();
				$rule      = str_replace( '%question_id%', $post->ID, $structure->rule );
				$rule      = str_replace( '%question%', $post->post_name, $rule );
				$link      = home_url( $default_lang . '/' . $rule . '/' );
			} else {
				$link = add_query_arg( array( 'question' => $post->ID ), ap_base_page_link() );
			}

			/**
			 * Allow overriding of question post type permalink
			 *
			 * @param string $link Question link.
			 * @param object $post Post object.
			 */
			return apply_filters( 'ap_question_post_type_link', $link, $post );
		} elseif ( 'answer' === $post->post_type && 0 !== (int) $post->post_parent ) {
			$link = get_permalink( $post->post_parent ) . "answer/{$post->ID}/";

			/**
			 * Allow overriding of answer post type permalink.
			 *
			 * @param string $link Question link.
			 * @param object $post Post object.
			 */
			return apply_filters( 'ap_answer_post_type_link', $link, $post );
		}

		return $link;
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment