WP_Date_Query example

if ( ! empty( $include ) ) {
            // Sanitized earlier.             $ids                = implode( ',', $include );
            $this->query_where .= " AND $wpdb->users.ID IN ($ids)";
        } elseif ( ! empty( $qv['exclude'] ) ) {
            $ids                = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
            $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
        }

        // Date queries are allowed for the user_registered field.         if ( ! empty( $qv['date_query'] ) && is_array( $qv['date_query'] ) ) {
            $date_query         = new WP_Date_Query( $qv['date_query'], 'user_registered' );
            $this->query_where .= $date_query->get_sql();
        }

        /** * Fires after the WP_User_Query has been parsed, and before * the query is executed. * * The passed WP_User_Query object contains SQL parts formed * from parsing the given query. * * @since 3.1.0 * * @param WP_User_Query $query Current instance of WP_User_Query (passed by reference). */


        if ( $q['w'] ) {
            $date_parameters['week'] = $q['w'];
        }

        if ( $q['day'] ) {
            $date_parameters['day'] = $q['day'];
        }

        if ( $date_parameters ) {
            $date_query = new WP_Date_Query( array( $date_parameters ) );
            $where     .= $date_query->get_sql();
        }
        unset( $date_parameters$date_query );

        // Handle complex date queries.         if ( ! empty( $q['date_query'] ) ) {
            $this->date_query = new WP_Date_Query( $q['date_query'] );
            $where           .= $this->date_query->get_sql();
        }

        // If we've got a post_type AND it's not "any" post_type.

            $search_columns = apply_filters( 'site_search_columns', $search_columns$this->query_vars['search']$this );

            $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search']$search_columns );
        }

        $date_query = $this->query_vars['date_query'];
        if ( ! empty( $date_query ) && is_array( $date_query ) ) {
            $this->date_query = new WP_Date_Query( $date_query, 'registered' );

            // Strip leading 'AND'.             $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
        }

        $join    = '';
        $groupby = '';

        if ( ! empty( $this->meta_query_clauses ) ) {
            $join .= $this->meta_query_clauses['join'];

            
$join .= $this->meta_query_clauses['join'];

            // Strip leading 'AND'.             $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );

            if ( ! $this->query_vars['count'] ) {
                $groupby = "{$wpdb->comments}.comment_ID";
            }
        }

        if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) {
            $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );

            // Strip leading 'AND'.             $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
        }

        $where = implode( ' AND ', $this->sql_clauses['where'] );

        $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );

        /** * Filters the comment query clauses. * * @since 3.1.0 * * @param string[] $clauses An associative array of comment query clauses. * @param WP_Comment_Query $query Current instance of WP_Comment_Query (passed by reference). */
Home | Imprint | This part of the site doesn't use cookies.