Advertisement
Mimeobrad

functions + cpt

Oct 29th, 2021
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.83 KB | None | 0 0
  1. //  FAQS CPT
  2. add_action( 'init', 'spc_faqs_custom_post_type' );
  3. function spc_faqs_custom_post_type()
  4. {
  5.     register_post_type( 'faqs',
  6.         array('labels' =>
  7.             array(
  8.                 'name' => __( 'FAQs' ),            
  9.                 'singular_name' => __( 'faqs' ),
  10.                 'menu_name' => __( 'FAQs' ),
  11.                 'all_items' => __( 'All FAQs' ),
  12.                 'add_new' => __( 'Add New FAQ' ),
  13.                 'add_new_item' => __( 'Add New Page to the FAQs' ),
  14.                 'edit_item' => __( 'Edit Page - faqs' ),
  15.                 'new_item' => __( 'Add New Page - faqs' ),
  16.                 'view_item' => __( 'View Page' ),
  17.                 'search_items' => __( 'Search faqs' ),
  18.                 'not_found' => __( 'No pages found' ),
  19.                 'not_found_in_trash' => __( 'No pages found in trash' )
  20.             ),
  21.             'public' => true,
  22.             'hierarchical' => true,
  23.             'has_archive' => true,
  24.             'exclude_from_search' => true,
  25.             'menu_icon' => 'dashicons-feedback',
  26.             'supports' =>
  27.                 array(
  28.                     'title',
  29.                     'editor',
  30.                     'page-attributes',                  
  31.                     'thumbnail',
  32.                     'custom-fields',
  33.                     'revisions',  
  34.             ),          
  35.             'capability_type' => 'post',
  36.             'rewrite' => array(
  37.                         'slug' => 'faqs',
  38.                         'with_front' => false // Permalinks format
  39.                     ),
  40.             'show_ui' => true,
  41.             'show_in_nav_menus' => true,
  42.             'show_in_menu' => true,
  43.             'menu_position' => 4,
  44.         )
  45.        
  46.     );
  47. }
  48.  
  49.  
  50. //  FAQS Taxes
  51. if ( ! function_exists( 'faq_taxonomy' ) ) {
  52.  
  53. // Register Custom Taxonomy
  54. function faq_taxonomy() {
  55.  
  56.     $labels = array(
  57.         'name'                       => _x( 'FAQ Categories', 'Taxonomy General Name', 'text_domain' ),
  58.         'singular_name'              => _x( 'FAQ Category', 'Taxonomy Singular Name', 'text_domain' ),
  59.         'menu_name'                  => __( 'FAQ Categories', 'text_domain' ),
  60.         'all_items'                  => __( 'All FAQ Categories', 'text_domain' ),
  61.         'parent_item'                => __( 'Parent FAQ Category', 'text_domain' ),
  62.         'parent_item_colon'          => __( 'Parent FAQ Category:', 'text_domain' ),
  63.         'new_item_name'              => __( 'New FAQ Category', 'text_domain' ),
  64.         'add_new_item'               => __( 'Add New FAQ Category', 'text_domain' ),
  65.         'edit_item'                  => __( 'Edit FAQ Category', 'text_domain' ),
  66.         'update_item'                => __( 'Update FAQ Category', 'text_domain' ),
  67.         'view_item'                  => __( 'View FAQ Category', 'text_domain' ),
  68.         'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
  69.         'add_or_remove_items'        => __( 'Add or remove FAQ Categories', 'text_domain' ),
  70.         'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
  71.         'popular_items'              => __( 'Popular FAQ Category', 'text_domain' ),
  72.         'search_items'               => __( 'SearchF AQ Category', 'text_domain' ),
  73.         'not_found'                  => __( 'FAQ Category Not Found', 'text_domain' ),
  74.         'no_terms'                   => __( 'No items', 'text_domain' ),
  75.         'items_list'                 => __( 'FAQ Category list', 'text_domain' ),
  76.         'items_list_navigation'      => __( 'FAQ Category list navigation', 'text_domain' ),
  77.     );
  78.     $args = array(
  79.         'labels'                     => $labels,
  80.         'hierarchical'               => true,
  81.         'public'                     => true,
  82.         'show_ui'                    => true,
  83.         'show_admin_column'          => true,
  84.         'show_in_nav_menus'          => true,
  85.         'show_tagcloud'              => true,
  86.     );
  87.     register_taxonomy( 'faq_cat', array( 'faqs' ), $args );
  88.  
  89. }
  90. add_action( 'init', 'faq_taxonomy', 0 );
  91.  
  92. }
  93. //  FAQS Filters
  94. function filter_faqs() {
  95. $termIds = array('1821','1820');
  96.   $ajaxposts = new WP_Query([
  97.     'post_type' => 'faqs',
  98.     'posts_per_page' => -1,
  99.     'orderby' => 'menu_order',
  100.     'order' => 'desc',
  101.     'tax_query' => [
  102.         [
  103.         'taxonomy' => 'faq_cat',
  104.          'field'    => 'term_id',
  105.          'terms'    =>  $termIds,
  106.          'operator' => 'IN'
  107.     ],
  108.     ]
  109.   ]);
  110.  
  111.   $response = '';
  112.  
  113.   if($ajaxposts->have_posts()) {
  114.     while($ajaxposts->have_posts()) : $ajaxposts->the_post();
  115.        $response .= get_template_part('templates/faq-single');
  116.     endwhile;
  117.   } else {
  118.     $response = 'empty';
  119.   }
  120.  
  121.   echo $response;
  122.   exit;
  123. }
  124. add_action('wp_ajax_filter_faqs', 'filter_faqs');
  125. add_action('wp_ajax_nopriv_filter_faqs', 'filter_faqs');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement