Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Script for getting posts
- function ajax_filter_get_posts( $taxonomy, $type ) {
- // Verify nonce
- if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
- die('Permission denied');
- $taxonomy = $_POST['taxonomy'];
- $type = $_POST['type']; // Post type
- // WP Query
- $args = array(
- 'tag' => $taxonomy,
- 'post_type' => $type, // Post type
- 'posts_per_page' => 10,
- );
- // If taxonomy is not set, remove key from array and get all posts
- if( !$taxonomy ) {
- unset( $args['tag'] );
- }
- $query = new WP_Query( $args );
- if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
- <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
- <?php the_excerpt(); ?>
- <?php endwhile; ?>
- <?php else: ?>
- <h2>No posts found</h2>
- <?php endif;
- die();
- }
- add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
- add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement