Skip to content

Instantly share code, notes, and snippets.

@rahularyan
Last active December 16, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahularyan/a44a989bb83af1c82c18c716993fbf55 to your computer and use it in GitHub Desktop.
Save rahularyan/a44a989bb83af1c82c18c716993fbf55 to your computer and use it in GitHub Desktop.
<?php
/**
* Filter AnsPress question permastruct.
*
* @param array $structure Question rules.
* @return array
*/
function my_ap_question_perm_structure( $structure ) {
add_rewrite_tag( '%question_category%', '([^/]+)' );
$question_slug = ap_get_page_slug( 'question' );
$structure['rule'] = ap_base_page_slug() . '' . $question_slug . '/%question_category%/%question%';
return $structure;
}
add_filter( 'ap_question_perm_structure', 'my_ap_question_perm_structure' );
/**
* Filter question link and replace %question_category% with first category slug.
*
* @param string $link
* @param WP_Post $post
* @return void
*/
function my_ap_question_post_type_link( $link, $post ) {
$cat = get_the_terms( $post->ID, 'question_category' )[0];
$link = str_replace( '%question_category%', $cat->slug, $link );
return $link;
}
add_filter( 'ap_question_post_type_link', 'my_ap_question_post_type_link', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment