Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: dp Filter
- Plugin URI: http://wpsv.se
- Description: Demonstration of filtering with query
- Version: 1.0
- Author: Kenth Hagström
- Author URI: http://designpalatset.se
- License: GPL
- Text Domain: dp-filter
- Domain Path: /_languages/
- */
- function dp_secondary_category() {
- $labels = array(
- 'name' => _x( 'Category 2', 'taxonomy general name' ),
- 'singular_name' => _x( 'Category 2', 'taxonomy singular name' ),
- 'search_items' => __( 'Search Category 2' ),
- 'all_items' => __( 'All Category 2' ),
- 'parent_item' => __( 'Parent Category 2' ),
- 'parent_item_colon' => __( 'Parent Category 2:' ),
- 'edit_item' => __( 'Edit Category 2' ),
- 'update_item' => __( 'Update Category 2' ),
- 'add_new_item' => __( 'Add New Category 2' ),
- 'new_item_name' => __( 'New Category 2 Name' ),
- 'menu_name' => __( 'Category 2' )
- );
- $args = array(
- 'hierarchical' => true,
- 'labels' => $labels,
- 'show_ui' => true,
- 'show_admin_column' => true,
- 'query_var' => true,
- 'rewrite' => array( 'slug' => 'category2' )
- );
- register_taxonomy( 'category2', 'post', $args );
- }
- add_action( 'init','dp_secondary_category' );
- function dp_filter() {
- global $query_string;
- if ( 'POST' == $_SERVER['REQUEST_METHOD'] && 'dp-do-filter' == $_POST['action'] ) {
- echo '<pre>';
- print_r($_POST);
- echo '</pre>';
- $args = wp_parse_args( $query_string );
- $cat = !empty( $_POST['cat'] ) ? $_POST['cat'] : array();
- $cat2 = !empty( $_POST['cat2'] ) ? $_POST['cat2'] : array();
- $paged = max( 1, get_query_var('paged') );
- $args['tax_query'] = array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'category',
- 'terms' => $cat,
- 'field' => 'id',
- ),
- array(
- 'taxonomy' => 'category2',
- 'terms' => $cat2,
- 'field' => 'id',
- ),
- );
- $filter = new WP_Query( $args );
- echo '<div style="background: #CCC;">';
- if( $filter->have_posts() ):
- echo '<ul>';
- while( $filter->have_posts() ) : $filter->the_post();
- echo '<li><a href="' . the_permalink() . '">' . the_title() . '</a></li>';
- endwhile;
- echo '</ul>';
- endif;
- wp_reset_query();
- echo '</div>';
- } else {
- ?>
- <form id="dp-filter" name="dp-filter" method="post" action="" class="dp-filter-form" enctype="multipart/form-data">
- <label for="dropdown_category"><?php _e( 'Category', 'dp-filter' ); ?>:</label>
- <?php
- $cat = wp_dropdown_categories( array( 'echo' => 0, 'taxonomy' => 'category', 'hide_empty' => 1 ) );
- $cat = str_replace( "name='cat' id=", "name='cat[]' id=", $cat );
- echo $cat;
- ?>
- <label for="dropdown_category2"><?php _e( 'Category2', 'dp-filter' ); ?>:</label>
- <?php
- $cat2 = wp_dropdown_categories( array( 'echo' => 0, 'taxonomy' => 'category2', 'hide_empty' => 1 ) );
- $cat2 = str_replace( "name='cat2' id=", "name='cat2[]' id=", $cat2 );
- echo $cat2;
- ?>
- <input type="submit" value="<?php _e( 'Filter', 'dp-filter' ); ?>" tabindex="40" id="submit" name="submit">
- <input type="hidden" name="action" value="dp-do-filter">
- </form>
- <?php
- }
- }
- add_shortcode( 'dp_filter', 'dp_filter' ); // [dp_filter]
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement