Advertisement
Guest User

BuddyPress Meta Query Woes

a guest
Dec 21st, 2014
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.61 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *
  5.  *
  6.  * This file defines and implements custom group fields for Great Books Great Discussions.
  7.  *
  8.  *  
  9.  */
  10.  
  11. function custom_field($meta_key='') {
  12.     //get current group id and load meta_key value if passed. If not pass it blank
  13.     return groups_get_groupmeta( bp_get_group_id(), $meta_key) ;
  14. }
  15.  
  16. //code if using seperate files require( dirname( __FILE__ ) . '/buddypress-group-meta.php' );
  17. // This function is our custom field's form that is called in create a group and when editing group details
  18. function group_header_fields_markup() {
  19.     global $bp, $wpdb;?>
  20.  
  21.    
  22.     <label for="group-custom-field-address">Address</label>
  23.     <input id="group-custom-field-address" type="text" name="group-custom-field-address" value="<?php echo custom_field('group-custom-field-address'); ?>" />
  24.  
  25.     <label for="group-custom-field-address-unit">Apt. / Unit / Suite:</label>
  26.     <input id="group-custom-field-address-unit" type="text" name="group-custom-field-address-unit" value="<?php echo custom_field('group-custom-field-address-unit'); ?>" />
  27.  
  28.     <label for="group-custom-field-city">City</label>
  29.     <input id="group-custom-field-city" type="text" name="group-custom-field-city" value="<?php echo custom_field('group-custom-field-city'); ?>" />   
  30.  
  31.     <label for="group-custom-field-state">State</label>
  32.     <input id="group-custom-field-state" type="text" name="group-custom-field-state" value="<?php echo custom_field('group-custom-field-state'); ?>" />
  33.  
  34.     <label for="group-custom-field-zip">Zip Code</label>
  35.     <input id="group-custom-field-zip" type="text" name="group-custom-field-zip" value="<?php echo custom_field('group-custom-field-zip'); ?>" />
  36.    
  37.     <br>
  38.  
  39. <?php }
  40.  
  41. // This saves the custom group meta – props to Boone for the function
  42. // Where $plain_fields = array.. you may add additional fields, eg
  43. //  $plain_fields = array(
  44. //      'field-one',
  45. //      'field-two'
  46. //  );
  47. function group_header_fields_save( $group_id ) {
  48.    
  49.     global $bp, $wpdb;
  50.  
  51.     $plain_fields = array(
  52.    
  53.         'group-custom-field-address',
  54.         'group-custom-field-address-unit',
  55.         'group-custom-field-city',
  56.         'group-custom-field-state',
  57.         'group-custom-field-zip',
  58.    
  59.     );
  60.  
  61.     foreach( $plain_fields as $field ) {
  62.    
  63.         $key = $field;
  64.        
  65.         if ( isset( $_POST[$key] ) ) {
  66.    
  67.             $value = $_POST[$key];
  68.    
  69.             groups_update_groupmeta( $group_id, $field, $value );
  70.         }
  71.     }
  72. }
  73.  
  74. add_filter( 'groups_custom_group_fields_editable', 'group_header_fields_markup' );
  75. add_action( 'groups_group_details_edited', 'group_header_fields_save' );
  76. add_action( 'groups_created_group',  'group_header_fields_save' );
  77.      
  78. // Show the custom field in the group header
  79. function show_field_in_header( ) {
  80.     echo "<p>Address:" . custom_field('group-custom-field-address') . "</p>";
  81.     echo "<p>Apt. / Unit / Suite:" . custom_field('group-custom-field-address-unit') . "</p>";
  82.     echo "<p>City:" . custom_field('group-custom-field-city') . "</p>";
  83.     echo "<p>State:" . custom_field('group-custom-field-state') . "</p>";
  84.     echo "<p>Zip code:" . custom_field('group-custom-field-zip') . "</p>";
  85. }
  86.  
  87. add_action('bp_group_header_meta' , 'show_field_in_header') ;
  88.  
  89. function gbgd_bpgf_filter_ajax_querystring( $querystring = '', $object = '' ) {
  90.  
  91.     /* bp_ajax_querystring is also used by other components, so you need
  92.     to check the object is groups, else simply return the querystring and stop the process */
  93.     if( $object != 'groups' )
  94.         return $querystring;
  95.  
  96.     // Let's rebuild the querystring as an array to ease the job
  97.     $defaults = array(
  98.         'type'            => 'active',
  99.         'action'          => 'active',
  100.         'scope'           => 'all',
  101.         'page'            => 1,
  102.         'user_id'         => 0,
  103.         'search_terms'    => '',
  104.         'exclude'         => false,
  105.     );
  106.  
  107.     $gbgd_bpgf_querystring = wp_parse_args( $querystring, $defaults );
  108.  
  109.     /* this is your meta_query */
  110.     $gbgd_bpgf_querystring['meta_query'] = array(
  111.         'relation' => 'OR',
  112.         array(
  113.             'key'     => 'group-custom-field-zip',
  114.             'value'   => $gbgd_bpgf_querystring["search_terms"],
  115.             'type'    => 'numeric',
  116.             'compare' => 'LIKE'
  117.         )
  118.     );
  119.  
  120.     echo '<pre>';
  121.     var_dump($gbgd_bpgf_querystring["search_terms"]);
  122.     echo '</pre>';
  123.  
  124.     // using a filter will help other plugins to eventually extend this feature
  125.     return apply_filters( 'gbgd_bpgf_filter_ajax_querystring', $gbgd_bpgf_querystring, $querystring );
  126. }
  127.  
  128. /* The groups loop uses bp_ajax_querystring( 'groups' ) to filter the groups
  129.    depending on the selected option */
  130. add_filter( 'bp_ajax_querystring', 'gbgd_bpgf_filter_ajax_querystring', 20, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement