Advertisement
deadlyhifi

WP_Query on custom taxonomy works fine but fails in wp_ajax_

Oct 25th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. <?php
  2. /* the class file mentioned on http://wordpress.stackexchange.com/questions/32088/wp-query-on-custom-taxonomy-works-fine-but-fails-if-run-through-wp-ajax */
  3.  
  4. $jttvvs = new jttv_ViewStats();
  5.  
  6. class jttv_ViewStats {
  7.  
  8.     function __construct()
  9.     {
  10.         add_action( 'admin_menu', array( $this, 'jttvvs_admin_menu') );
  11.         add_action('wp_ajax_get_views', array( $this, 'get_views') );
  12.     }
  13.  
  14.     /**
  15.      * Add the admin page
  16.      */
  17.     function jttvvs_admin_menu()
  18.     {
  19.         $page = add_submenu_page('jimmyteenstv', __('View Stats'), __('View Stats'), 'edit_themes', 'jttv_viewstats', array( $this, 'jttvvs_settings_page') );
  20.         add_action("admin_print_scripts-$page", array( $this, 'jttvvs_js') );
  21.         add_action("admin_print_scripts-$page", array( $this, 'jttvvs_css') );
  22.     }
  23.  
  24.     /**
  25.      * load the js
  26.      */
  27.     function jttvvs_js()
  28.     {
  29.       wp_enqueue_script( "jttv_viewstats", WP_PLUGIN_URL .  "/stats/js/jttv_viewstats.js", array( 'jquery' ) );
  30.     }
  31.    
  32.     /**
  33.      * load css
  34.      */
  35.     function jttvvs_css()
  36.     {
  37.     ?>
  38. <style type="text/css">span.loader { background: transparent url( WP_PLUGIN_URL .  "/stats/assets/loader.gif") no-repeat 0 0; width:16px; height:11px; display: block; margin:3px 0 0 15px;}</style>
  39.     <?php
  40.     }
  41.  
  42.     /**
  43.      * ajax return
  44.      */
  45.     function get_views()
  46.     {
  47.         $args = array(
  48.             'posts_per_page'=> -1,
  49.             'post_status' => 'publish'
  50.         );
  51.  
  52.         if ( isset($_POST['tag_id']) ) :
  53.             $args['tax_query'] =
  54.                 array(array(
  55.                     'taxonomy' => 'region',
  56.                     'field' => 'id',
  57.                     'terms' => $_POST['tax_id'],
  58.                 ));
  59.         endif;
  60.  
  61. //echo '<pre>' . print_r($args, true) . '</pre>';
  62.  
  63.         $the_query = new WP_Query($args);
  64.  
  65.         while ( $the_query->have_posts() ) : $the_query->the_post();
  66.  
  67.             $reg = get_post_meta(get_the_ID(), 'reg_count');
  68.             $unreg = get_post_meta(get_the_ID(), 'unreg_count');   
  69.             $reg_count = $reg_count + $reg[0];
  70.             $unreg_count = $unreg_count + $unreg[0];
  71.         endwhile;
  72.  
  73.         echo "total: " . number_format($reg_count + $unreg_count);
  74.  
  75.         die();
  76.     }
  77.  
  78.     /**
  79.      * non ajax return
  80.      */
  81.     function get_views_nojax($tax_id = false)
  82.     {
  83.         $args = array(
  84.             'numberposts' => -1,
  85.             'post_status' => 'publish'
  86.         );
  87.         if ( isset($tax_id) ) :
  88.             $args['tax_query'] =
  89.                 array(
  90.                     array(
  91.                         'taxonomy' => 'region',
  92.                         'field' => 'id',
  93.                         'terms' => $tax_id
  94.                 );
  95.            
  96.         endif;
  97.  
  98.         $posts = get_posts( $args );
  99.  
  100.         foreach ( $posts as $post ) :
  101.  
  102.             $reg = get_post_meta($post->ID, 'reg_count');
  103.             $unreg = get_post_meta($post->ID, 'unreg_count');  
  104.             $reg_count = $reg_count + $reg[0];
  105.             $unreg_count = $unreg_count + $unreg[0];
  106.  
  107.         endforeach;
  108.  
  109.         return "total " . number_format($reg_count + $unreg_count);
  110.     }
  111.  
  112.     /**
  113.      * the regions
  114.      */
  115.     function list_regions()
  116.     {
  117.         // regions
  118.         $args = array(
  119.             'orderby' => 'name',
  120.             'order' => 'ASC',
  121.             'taxonomy' => 'region',
  122.             'child_of' => 0
  123.         );
  124.  
  125.         $cats = get_categories($args);
  126.  
  127.         foreach ( $cats as $cat ) :
  128.  
  129.             echo '<tr>';
  130.                 echo '<td>' . $cat->cat_ID . " " . $cat->name . '</td>';
  131.                 echo '<td id="' . $cat->cat_ID . '">' . $this->get_views_nojax($cat->cat_ID) . '</td>';
  132.                 echo '<td id="' . $cat->cat_ID . '"><a class="button-secondary getviews">Count Views</a></td>';
  133.             echo '</tr>';
  134.  
  135.         endforeach;
  136.     }
  137.  
  138.     /**
  139.      * Create the viewable page
  140.      */
  141.     function jttvvs_settings_page()
  142.     {
  143.     ?>
  144.         <div class="wrap">
  145.    
  146.             <div id="icon-options-general" class="icon32"></div><h2>Film View Stats</h2>
  147.             <table class="widefat">
  148.             <tr>
  149.                 <td>Total Views:</td>
  150.                 <td><?php echo $this->get_views_nojax($cat->cat_ID); ?></td>
  151.                 <td width="50%"><a class="button-secondary getviews">Count Views</a></td>
  152.             </tr>
  153.             <tr>
  154.                 <td colspan="3">&nbsp;</td>
  155.             </tr>
  156.             <?php $this->list_regions(); ?>
  157.             </table>
  158.  
  159.         </div>
  160.    
  161.     <?php
  162.     }
  163.  
  164. }//
  165.  
  166.  
  167. ?>
  168.  
  169. /* the jquery - in a separate file - loaded through function jttvvs_js() */
  170. jQuery(document).ready( function($) {
  171.     $("a.getviews").click( function() {
  172.         var td = $(this).parent();
  173.    
  174.         /* only fetch results once */
  175.         $(this).unbind('click').bind('click', function(){return false;});
  176.    
  177.         // replace button with loader
  178.         $(td).html('<span class="loader"></span>');
  179.    
  180.         $.post($(this), {
  181.                 action: "get_views",
  182.                 tax_id: td.attr("id")
  183.             }, function(data) {
  184.                 td.html(data);
  185.             }
  186.         );
  187.         return false;
  188.     });
  189. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement