daily pastebin goal
35%
SHARE
TWEET

Query Metaboxes with WPAlchemy (without extract mode)

a guest Dec 10th, 2010 289 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* DEFINING THE METABOX
  2.  * note I have used FUNCTIONS_PATH- this is a constant i've set in my theme, so you'd need to adjust accordingly
  3. */
  4.  
  5.  
  6. $query_metabox = new WPAlchemy_MetaBox(array
  7. (
  8.         'id' => '_query_meta', // underscore prefix hides fields from the custom fields area
  9.         'title' => _('Custom Query Arguments'),
  10.         'template' => FUNCTIONS_PATH .'WPAlchemy/query_meta.php',
  11.         'prefix' => '_query_meta_',
  12.         'include_template' => array('template-page-blog.php','template-page-home.php'),
  13.         'priority' => 'high',
  14.        
  15.        
  16. ));
  17.  
  18. /* QUERY_META.PHP
  19.  * contents of my metaboxes- multi group that asks for query parameters and their respective value
  20. * I use a little jquery to hide the normal post box if I don't want any content shown
  21. */
  22.  
  23. <script type='text/javascript'>
  24. /* <![CDATA[ */
  25.  
  26. //avoid jquery flicker
  27.         document.write('<style type="text/css">body{display:none}</style>');
  28.  
  29.         jQuery(document).ready(function($) {
  30.  
  31.         //hide normal text editor when editing page running blog template
  32.         var template = $('select#page_template').val();
  33.        
  34.         if( template == "template-page-blog.php"  ) {
  35.                 $('#postdivrich').hide();
  36.         }
  37.  
  38.         //show page after loading
  39.         $('body').css('display','block');
  40.        
  41. });
  42. /* ]]> */
  43. </script>
  44.  
  45. <div class="my_meta_control">
  46.  
  47.         <p><?php _e('This page is running a blog-style template and will display all your posts unless you modify the query posts arguments below.  ');?></p>
  48.  
  49.         <p><?php _e('See the ');?><a href="http://codex.wordpress.org/Function_Reference/query_posts"><?php _e('Wordpress Codex Function Reference');?></a> <?php _e('for more details on helpful parameters.')?><?php _e('Please be careful and leave this blank if you are not comfortable with query parameters.')?></p>
  50.  
  51.  
  52.         <h4><?php _e('Query Arguments');?></h4>
  53.  
  54.         <a style="float:right; margin:0 10px;" href="#" class="dodelete-args button"><?php _e('Remove All Arguments');?></a>
  55.  
  56.         <p><?php _e('Add arguments to the custom query by entering in the parameter name and the corresponsding parameter value.  Add new arguments by using the "Add Argument" button.');?></p>
  57.  
  58.        
  59.         <?php while($mb->have_fields_and_multi('args')): ?>
  60.         <?php $mb->the_group_open(); ?>
  61.        
  62.         <table>
  63.                 <tr>
  64.                         <td><p style="margin-bottom:0"><span><?php _e('Parameter');?></span></p></td>
  65.                         <td><p style="margin-bottom:0"><span><?php _e('Value');?></span></p></td>
  66.                 </tr>
  67.                
  68.         <tr>
  69.         <td>
  70.                 <?php $mb->the_field('param_key'); ?>
  71.                 <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
  72.         </td>
  73.         <td>
  74.                 <?php $mb->the_field('param_value'); ?>
  75.                 <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
  76.         </td>
  77.  
  78.         <td>   
  79.                 <p><a href="#" class="dodelete button">Remove</a></p>
  80.                
  81.         </td>
  82.         </tr>
  83.         </table>
  84.        
  85.        
  86.         <?php $mb->the_group_close(); ?>
  87.         <?php endwhile; ?>
  88.  
  89.         <p style="margin-bottom:15px; padding-top:5px;"><a href="#" class="docopy-args button">Add Argument</a></p>
  90.        
  91.  
  92. </div>
  93.  
  94.  
  95.  
  96. /* MODIFYING THE QUERY BASED ON THE RESULTS
  97. * essentially I loop the values I got from the metaboxes to create an array
  98. * then i merge this array with some default to create a new array that will serve
  99. * as the query parameters */
  100.  
  101.  
  102. $user_args = array();
  103.                        
  104. // loop a set of field groups to create user query
  105.                                
  106. while($query_metabox->have_fields('args')) {
  107.         $user_args[$query_metabox->get_the_value('param_key')] = $query_metabox->get_the_value('param_value') ;
  108. }
  109.                        
  110. /*solves the problem with pagination on a static page*/
  111.         if ( get_query_var('paged') ) {
  112.                 $paged = get_query_var('paged');
  113.         } elseif ( get_query_var('page') ) {
  114.                 $paged = get_query_var('page');
  115.         } else {
  116.                 $paged = 1;
  117.         }
  118.                                
  119. $default_args = array('posts_per_page' => 5, 'paged' => $paged );
  120. $query_args = array_merge($default_args, $user_args);
  121.        
  122. $wp_query = new WP_Query();            
  123. $wp_query->query( $query_args );
  124. $more = 0;
  125.                        
  126. /* just a note that you can retrieve the current, default WP query by accessing the global object variable $wp_query
  127.  
  128. $default_args = $wp_query->query_vars;
  129. $my_args = array('posts_per_page'=>6);
  130. $new_args = array_merge($default_args, $my_args);
  131.  
  132. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top