bp_filter_metaid_column_name

Filter SQL query strings to swap out the 'meta_id' column.

Description

(string) bp_filter_metaid_column_name( (string) $q ); 

WordPress uses the meta_id column for commentmeta and postmeta, and so hardcodes the column name into its *_metadata() functions. BuddyPress, on the other hand, uses id for the primary column. To make WP's functions usable for BuddyPress, we use this just-in-time filter on query to swap 'meta_id' with 'id.

Returns (string)

Parameters (1)

0. $q (string)
SQL query.

Usage

  1. if ( !function_exists( 'bp_filter_metaid_column_name' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-filters.php'; 
  3.  
  4. // SQL query. 
  5. $q = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_filter_metaid_column_name($q); 
  9.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-filters.php  
  1. function bp_filter_metaid_column_name( $q ) { 
  2. /** 
  3. * Replace quoted content with __QUOTE__ to avoid false positives. 
  4. * This regular expression will match nested quotes. 
  5. */ 
  6. $quoted_regex = "/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'/s"; 
  7. preg_match_all( $quoted_regex, $q, $quoted_matches ); 
  8. $q = preg_replace( $quoted_regex, '__QUOTE__', $q ); 
  9.  
  10. $q = str_replace( 'meta_id', 'id', $q ); 
  11.  
  12. // Put quoted content back into the string. 
  13. if ( ! empty( $quoted_matches[0] ) ) { 
  14. for ( $i = 0; $i < count( $quoted_matches[0] ); $i++ ) { 
  15. $quote_pos = strpos( $q, '__QUOTE__' ); 
  16. $q = substr_replace( $q, $quoted_matches[0][ $i ], $quote_pos, 9 ); 
  17.  
  18. return $q;