SHARE
TWEET

Untitled

5ally Aug 21st, 2018 (edited) 38 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // See https://wordpress.stackexchange.com/a/306993/137402 for details.
  3.  
  4. add_action( 'init', 'my_register_questions_post_types' );
  5. function my_register_questions_post_types() {
  6.     register_post_type( 'english-speaking', array(
  7.         'label' => 'English Speaking',
  8.         'public' => true,
  9.         'supports' => [ 'title', 'editor', 'custom-fields' ],
  10.         'rewrite' => array(
  11.             'slug' => 'english/speaking/%speaking_task%/%question_id%',
  12.         ),
  13.         // Other args here.
  14.     ) );
  15.  
  16.     register_post_type( 'english-writing', array(
  17.         'label' => 'English Writing',
  18.         'public' => true,
  19.         'supports' => [ 'title', 'editor', 'custom-fields' ],
  20.         'rewrite' => array(
  21.             'slug' => 'english/writing/%question_id%',
  22.         ),
  23.         // Other args here.
  24.     ) );
  25. }
  26.  
  27. add_action( 'init', 'my_register_questions_taxonomies' );
  28. function my_register_questions_taxonomies() {
  29.     register_taxonomy( 'speaking-task', array( 'english-speaking' ), array(
  30.         'label' => 'Speaking Tasks',
  31.         'public' => true,
  32.         'hierarchical' => true,
  33.         'rewrite' => array(
  34.             'slug' => 'english/speaking',
  35.         ),
  36.         // Other args here.
  37.     ) );
  38. }
  39.  
  40. add_action( 'init', 'my_register_questions_rewrite_tags' );
  41. function my_register_questions_rewrite_tags() {
  42.     add_rewrite_tag( '%question_id%', '(\d+)' );
  43.     add_rewrite_tag( '%speaking_task%', '([^/]+)' );
  44. }
  45.  
  46. add_filter( 'post_type_link', 'my_filter_questions_post_type_link', 10, 2 );
  47. function my_filter_questions_post_type_link( $post_link, $post ) {
  48.     // Replaces/rewrites %question_id% in the permalink.
  49.     if ( false !== strpos( $post_link, '%question_id%' ) ) {
  50.         $id = get_post_meta( $post->ID, 'wpcf-question-id', true );
  51.  
  52.         // A default value is necessary, and the value has to be a 0.
  53.         $id = $id ? $id : '0';
  54.  
  55.         $post_link = str_replace( '%question_id%', $id, $post_link );
  56.     }
  57.  
  58.     // Replaces/rewrites %speaking_task% in the permalink.
  59.     if ( false !== strpos( $post_link, '%speaking_task%' ) ) {
  60.         // A default value is necessary, but the term/category doesn't need to
  61.         // actually exists. So you could, for example, use 'all' as the value.
  62.         $slug = 'uncategorized';
  63.  
  64.         $cats = get_the_terms( $post, 'speaking-task' );
  65.         if ( $cats && ! is_wp_error( $cats ) ) {
  66.             $slug = $cats[0]->slug;
  67.         }
  68.  
  69.         $post_link = str_replace( '%speaking_task%', $slug, $post_link );
  70.     }
  71.  
  72.     return $post_link;
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top