Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. <?php
  2. function vb_filter_posts() {
  3.  
  4. if( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'bobz' ) )
  5. die('Permission denied');
  6.  
  7. /**
  8. * Default response
  9. */
  10. $response = [
  11. 'status' => 500,
  12. 'message' => 'Something is wrong, please try again later ...',
  13. 'content' => false,
  14. 'found' => 0
  15. ];
  16.  
  17. $tax = sanitize_text_field($_POST['params']['tax']);
  18. $term = sanitize_text_field($_POST['params']['term']);
  19. $page = intval($_POST['params']['page']);
  20. $qty = intval($_POST['params']['qty']);
  21.  
  22. /**
  23. * Check if term exists
  24. */
  25. if (!term_exists( $term, $tax)) :
  26. $response = [
  27. 'status' => 501,
  28. 'message' => 'Term doesn\'t exist',
  29. 'content' => 0
  30. ];
  31.  
  32. die(json_encode($response));
  33. endif;
  34.  
  35. /**
  36. * Setup query
  37. */
  38. $args = [
  39. 'paged' => $page,
  40. 'post_type' => 'any',
  41. 'post_status' => 'published',
  42. 'posts_per_page' => $qty,
  43. 'tax_query' => [
  44. [
  45. 'taxonomy' => $tax,
  46. 'field' => 'slug',
  47. 'terms' => $term,
  48. ]
  49. ],
  50. ];
  51.  
  52. $qry = new WP_Query($args);
  53.  
  54. ob_start();
  55. if ($qry->have_posts()) :
  56. while ($qry->have_posts()) : $qry->the_post(); ?>
  57.  
  58. <article class="loop-item">
  59. <header>
  60. <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  61. </header>
  62. <div class="entry-summary">
  63. <?php the_excerpt(); ?>
  64. </div>
  65. </article>
  66.  
  67. <?php endwhile;
  68.  
  69. /**
  70. * Pagination
  71. */
  72. vb_ajax_pager($qry,$page);
  73.  
  74. $response = [
  75. 'status'=> 200,
  76. 'found' => $qry->found_posts
  77. ];
  78.  
  79.  
  80. else :
  81.  
  82. $response = [
  83. 'status' => 201,
  84. 'message' => 'No posts found'
  85. ];
  86.  
  87. endif;
  88.  
  89. $response['content'] = ob_get_clean();
  90.  
  91. die(json_encode($response));
  92.  
  93. }
  94. add_action('wp_ajax_do_filter_posts', 'vb_filter_posts');
  95. add_action('wp_ajax_nopriv_do_filter_posts', 'vb_filter_posts');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement