BP_XProfile_Meta_Query::get_sql( string $type, string $primary_table, string $primary_id_column, object|null $context = null )

Generates SQL clauses to be appended to a main query.

Description

Parameters

$type

(Required) Type of meta, eg 'user', 'post'.

$primary_table

(Required) Database table where the object being filtered is stored (eg wp_users).

$primary_id_column

(Required) ID column for the filtered object in $primary_table.

$context

(Optional) The main query object.

Default value: null

Return

(array) Array containing JOIN and WHERE SQL clauses to append to the main query.

  • 'join'
    (string) SQL fragment to append to the main JOIN clause.
  • 'where'
    (string) SQL fragment to append to the main WHERE clause.

Source

File: bp-xprofile/classes/class-bp-xprofile-meta-query.php

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
    if ( ! $meta_table = _get_meta_table( $type ) ) {
        return false;
    }
 
    $this->meta_table     = $meta_table;
    $this->meta_id_column = 'object_id';
 
    $this->primary_table     = $primary_table;
    $this->primary_id_column = $primary_id_column;
 
    $sql = $this->get_sql_clauses();
 
    /*
     * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should
     * be LEFT. Otherwise posts with no metadata will be excluded from results.
     */
    if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) {
        $sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] );
    }
 
    /**
     * Filter the meta query's generated SQL.
     *
     * @since BuddyPress 2.3.0
     *
     * @param array $args {
     *     An array of meta query SQL arguments.
     *
     *     @type array  $clauses           Array containing the query's JOIN and WHERE clauses.
     *     @type array  $queries           Array of meta queries.
     *     @type string $type              Type of meta.
     *     @type string $primary_table     Primary table.
     *     @type string $primary_id_column Primary column ID.
     *     @type object $context           The main query object.
     * }
     */
    return apply_filters_ref_array( 'bp_xprofile_get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
}

Changelog

Changelog
Version Description
BuddyPress 2.3.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.