Advertisement
keha76

WordPress Content Filter

Feb 14th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: dp Filter
  4. Plugin URI: http://wpsv.se
  5. Description: Demonstration of filtering with query
  6. Version: 1.0
  7. Author: Kenth Hagström
  8. Author URI: http://designpalatset.se
  9. License: GPL
  10. Text Domain: dp-filter
  11. Domain Path: /_languages/
  12. */
  13.  
  14. function dp_secondary_category() {
  15.    
  16.     $labels = array(
  17.         'name'                => _x( 'Category 2', 'taxonomy general name' ),
  18.         'singular_name'       => _x( 'Category 2', 'taxonomy singular name' ),
  19.         'search_items'        => __( 'Search Category 2' ),
  20.         'all_items'           => __( 'All Category 2' ),
  21.         'parent_item'         => __( 'Parent Category 2' ),
  22.         'parent_item_colon'   => __( 'Parent Category 2:' ),
  23.         'edit_item'           => __( 'Edit Category 2' ),
  24.         'update_item'         => __( 'Update Category 2' ),
  25.         'add_new_item'        => __( 'Add New Category 2' ),
  26.         'new_item_name'       => __( 'New Category 2 Name' ),
  27.         'menu_name'           => __( 'Category 2' )
  28.     );
  29.  
  30.     $args = array(
  31.         'hierarchical'        => true,
  32.         'labels'              => $labels,
  33.         'show_ui'             => true,
  34.         'show_admin_column'   => true,
  35.         'query_var'           => true,
  36.         'rewrite'             => array( 'slug' => 'category2' )
  37.     );
  38.  
  39.     register_taxonomy( 'category2', 'post', $args );
  40. }
  41.  
  42. add_action( 'init','dp_secondary_category' );
  43.  
  44.  
  45. function dp_filter() {
  46.    
  47.     global $query_string;
  48.    
  49.     if ( 'POST' == $_SERVER['REQUEST_METHOD'] && 'dp-do-filter' == $_POST['action'] ) {
  50.        
  51.         echo '<pre>';
  52.         print_r($_POST);
  53.         echo '</pre>';
  54.        
  55.         $args = wp_parse_args( $query_string );
  56.        
  57.         $cat = !empty( $_POST['cat'] ) ? $_POST['cat'] : array();
  58.         $cat2 = !empty( $_POST['cat2'] ) ? $_POST['cat2'] : array();
  59.    
  60.         $paged = max( 1, get_query_var('paged') );
  61.    
  62.         $args['tax_query'] = array(
  63.             'relation' => 'OR',
  64.             array(
  65.                 'taxonomy' => 'category',
  66.                 'terms' => $cat,
  67.                 'field' => 'id',
  68.             ),
  69.             array(
  70.                 'taxonomy' => 'category2',
  71.                 'terms' => $cat2,
  72.                 'field' => 'id',
  73.             ),
  74.         );
  75.    
  76.         $filter = new WP_Query( $args );
  77.        
  78.         echo '<div style="background: #CCC;">';
  79.         if( $filter->have_posts() ):
  80.            
  81.             echo '<ul>';
  82.             while( $filter->have_posts() ) : $filter->the_post();
  83.                 echo '<li><a href="' . the_permalink() . '">' . the_title() . '</a></li>';
  84.             endwhile;
  85.             echo '</ul>';
  86.            
  87.         endif;
  88.         wp_reset_query();
  89.         echo '</div>';
  90.        
  91.     } else {
  92.     ?>
  93.     <form id="dp-filter" name="dp-filter" method="post" action="" class="dp-filter-form" enctype="multipart/form-data">
  94.         <label for="dropdown_category"><?php _e( 'Category', 'dp-filter' ); ?>:</label>
  95.         <?php
  96.         $cat = wp_dropdown_categories( array( 'echo' => 0, 'taxonomy' => 'category', 'hide_empty' => 1 ) );
  97.         $cat = str_replace( "name='cat' id=", "name='cat[]' id=", $cat );
  98.         echo $cat;
  99.         ?>
  100.        
  101.         <label for="dropdown_category2"><?php _e( 'Category2', 'dp-filter' ); ?>:</label>
  102.         <?php
  103.         $cat2 = wp_dropdown_categories( array( 'echo' => 0, 'taxonomy' => 'category2', 'hide_empty' => 1 ) );
  104.         $cat2 = str_replace( "name='cat2' id=", "name='cat2[]' id=", $cat2 );
  105.         echo $cat2;
  106.         ?>
  107.        
  108.         <input type="submit" value="<?php _e( 'Filter', 'dp-filter' ); ?>" tabindex="40" id="submit" name="submit">
  109.         <input type="hidden" name="action" value="dp-do-filter">
  110.        
  111.     </form>
  112.     <?php
  113.     }
  114. }
  115.  
  116. add_shortcode( 'dp_filter', 'dp_filter' ); // [dp_filter]
  117.  
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement