Bobz

Untitled

Apr 23rd, 2014
528
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Script for getting posts
  2. function ajax_filter_get_posts( $taxonomy, $type ) {
  3.  
  4.   // Verify nonce
  5.   if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
  6.     die('Permission denied');
  7.  
  8.   $taxonomy = $_POST['taxonomy'];
  9.   $type = $_POST['type']; // Post type
  10.  
  11.   // WP Query
  12.   $args = array(
  13.     'tag' => $taxonomy,
  14.     'post_type' => $type, // Post type
  15.     'posts_per_page' => 10,
  16.   );
  17.  
  18.   // If taxonomy is not set, remove key from array and get all posts
  19.   if( !$taxonomy ) {
  20.     unset( $args['tag'] );
  21.   }
  22.  
  23.   $query = new WP_Query( $args );
  24.  
  25.   if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
  26.  
  27.     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  28.     <?php the_excerpt(); ?>
  29.  
  30.   <?php endwhile; ?>
  31.   <?php else: ?>
  32.     <h2>No posts found</h2>
  33.   <?php endif;
  34.  
  35.   die();
  36. }
  37.  
  38. add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
  39. add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
RAW Paste Data