/** * Parse and sanitize 'orderby' keys passed to the user query. * * @since 4.2.0 * @access protected * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $orderby Alias for the field to order by. * @return string Value to used in the ORDER clause, if `$orderby` is valid. */ protected function parse_orderby($orderby) { global $wpdb; $meta_query_clauses = $this->meta_query->get_clauses(); $_orderby = ''; if (in_array($orderby, array('login', 'nicename', 'email', 'url', 'registered'))) { $_orderby = 'user_' . $orderby; } elseif (in_array($orderby, array('user_login', 'user_nicename', 'user_email', 'user_url', 'user_registered'))) { $_orderby = $orderby; } elseif ('name' == $orderby || 'display_name' == $orderby) { $_orderby = 'display_name'; } elseif ('post_count' == $orderby) { // todo: avoid the JOIN $where = get_posts_by_author_sql('post'); $this->query_from .= " LEFT OUTER JOIN (\n\t\t\t\tSELECT post_author, COUNT(*) as post_count\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t{$where}\n\t\t\t\tGROUP BY post_author\n\t\t\t) p ON ({$wpdb->users}.ID = p.post_author)\n\t\t\t"; $_orderby = 'post_count'; } elseif ('ID' == $orderby || 'id' == $orderby) { $_orderby = 'ID'; } elseif ('meta_value' == $orderby || $this->get('meta_key') == $orderby) { $_orderby = "{$wpdb->usermeta}.meta_value"; } elseif ('meta_value_num' == $orderby) { $_orderby = "{$wpdb->usermeta}.meta_value+0"; } elseif ('include' === $orderby && !empty($this->query_vars['include'])) { $include = wp_parse_id_list($this->query_vars['include']); $include_sql = implode(',', $include); $_orderby = "FIELD( {$wpdb->users}.ID, {$include_sql} )"; } elseif (isset($meta_query_clauses[$orderby])) { $meta_clause = $meta_query_clauses[$orderby]; $_orderby = sprintf("CAST(%s.meta_value AS %s)", esc_sql($meta_clause['alias']), esc_sql($meta_clause['cast'])); } return $_orderby; }
/** * count user post by post type * @param (integer) $userID (required) The ID of the user to count posts for. * @param (string) $post_type The post type you want to count, Default is post. * @version 1.0 * @author dakachi * @package AE * http://codex.wordpress.org/Function_Reference/count_user_posts */ function ae_count_user_posts_by_type($userid, $post_type = 'post') { global $wpdb; $where = get_posts_by_author_sql($post_type, true, $userid); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); return apply_filters('get_usernumposts', $count, $userid); }
function amr_count_user_posts($userid, $post_type) { // wordpress function does not allow for custom post types global $wpdb; if (!post_type_exists($post_type)) { return false; } $where = get_posts_by_author_sql($post_type, true, $userid); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); return apply_filters('get_usernumposts', $count, $userid); }
function count_user_posts_by_type($user_id = '', $post_type = 'post') { global $wpdb; if (!$user_id) { $user_id = um_user('ID'); } if (!$user_id) { return 0; } $where = get_posts_by_author_sql($post_type, true, $user_id); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); return apply_filters('um_pretty_number_formatting', $count); }
function ssmf_count_user_posts_by_type($userid, $post_type = 'subscribe_me_forms') { global $wpdb; $userid = get_current_user_id(); $where = get_posts_by_author_sql($post_type, true, $userid); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); $screen = get_current_screen(); if (current_user_can('edit_posts') and $screen->post_type === 'subscribe_me_forms') { //Is admin and all users - so impose the limit if ($count >= 2) { header("Location: /wp-content/plugins/mailchimp-subscribe-sm/phuf.php"); } } }
function mpsp_count_user_posts_by_type($userid, $post_type = 'mpsp_slider') { global $wpdb; $userid = get_current_user_id(); $where = get_posts_by_author_sql($post_type, true, $userid); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); $screen = get_current_screen(); if (current_user_can('edit_posts') and $screen->post_type === 'mpsp_slider') { //Is admin and all users - so impose the limit if ($count >= 300) { header("Location: /wp-content/plugins/Posts-Slider-NEW/phuf.php"); } } }
/** * Count user posts by post type * @param int $userid * @param string $post_type * @return int * @since unknown */ function ap_count_user_posts_by_type($userid, $post_type = 'question') { global $wpdb; $where = get_posts_by_author_sql($post_type, true, $userid); $query = "SELECT COUNT(*) FROM {$wpdb->posts} {$where}"; $key = md5($query); $cache = wp_cache_get($key, 'count'); if ($cache === false) { $count = $wpdb->get_var($query); wp_cache_set($key, $count, 'count'); } else { $count = $cache; } return apply_filters('ap_count_user_posts_by_type', $count, $userid); }
/** * Gets related posts by taxonomy. * * @since 0.1 * * @global object $wpdb * * @param int $post_id The post id to get related posts for. * @param array|string $taxonomies The taxonomies to retrieve related posts from * @param array|string $args Optional. Change what is returned * @return array Empty array if no related posts found. Array with post objects. */ function km_rpbt_related_posts_by_taxonomy($post_id = 0, $taxonomies = 'category', $args = '') { global $wpdb; if (!absint($post_id)) { return array(); } $defaults = array('post_types' => 'post', 'posts_per_page' => 5, 'order' => 'DESC', 'fields' => '', 'limit_posts' => -1, 'limit_year' => '', 'limit_month' => '', 'orderby' => 'post_date', 'exclude_terms' => '', 'include_terms' => '', 'exclude_posts' => '', 'post_thumbnail' => '', 'related' => true); $args = wp_parse_args($args, $defaults); $taxonomies = !empty($taxonomies) ? $taxonomies : array('category'); if (!is_array($taxonomies)) { $taxonomies = array_unique(explode(',', (string) $taxonomies)); } $terms = array(); // validates ids and returns an array $included = km_rpbt_related_posts_by_taxonomy_validate_ids($args['include_terms']); if (!$args['related'] && !empty($included)) { // related, use included term ids $terms = $included; } else { // related and not related terms $terms = wp_get_object_terms($post_id, array_map('trim', (array) $taxonomies), array('fields' => 'ids')); if (is_wp_error($terms) || empty($terms)) { return array(); } // only use included terms from the post terms if ($args['related'] && !empty($included)) { $terms = array_values(array_intersect($included, $terms)); } } // exclude terms if (empty($included)) { // validates ids and returns an array $excluded = km_rpbt_related_posts_by_taxonomy_validate_ids($args['exclude_terms']); $terms = array_values(array_diff($terms, $excluded)); } if (empty($terms)) { return array(); } $args['related_terms'] = $terms; // term ids sql if (count($terms) > 1) { $term_ids_sql = "tt.term_id IN (" . implode(', ', $terms) . ")"; } else { $term_ids_sql = isset($terms[0]) ? "tt.term_id = " . $terms[0] : "tt.term_id = 0"; } // validates ids and returns an array $exclude_posts = km_rpbt_related_posts_by_taxonomy_validate_ids($args['exclude_posts']); // add current post ID to exclude $exclude_posts[] = $post_id; $exclude_posts = array_unique($exclude_posts); // post ids sql $post_ids_sql = "AND {$wpdb->posts}.ID"; if (count($exclude_posts) > 1) { $post_ids_sql .= " NOT IN (" . implode(', ', $exclude_posts) . ")"; } else { $post_ids_sql .= " != {$post_id}"; } // post types if (!is_array($args['post_types'])) { $args['post_types'] = explode(',', (string) $args['post_types']); } // sanitize post type names and remove duplicates $post_types = array_unique(array_map('sanitize_key', (array) $args['post_types'])); $post_types = array_filter($post_types, 'post_type_exists'); // default to post type post if no post types are found $post_types = !empty($post_types) ? $post_types : array('post'); // where sql (post types and post status) if (count($post_types) > 1) { $where = get_posts_by_author_sql('post'); $post_type_sql = "'" . implode("', '", $post_types) . "'"; $where_sql = preg_replace("/post_type = 'post'/", "post_type IN ({$post_type_sql})", $where); } else { $where_sql = get_posts_by_author_sql($post_types[0]); } $order_by_rand = false; // order sql switch (strtoupper((string) $args['order'])) { case 'ASC': $order_sql = 'ASC'; break; case 'RAND': $order_sql = 'RAND()'; $order_by_rand = true; break; default: $order_sql = 'DESC'; break; } $allowed_fields = array('ids' => 'ID', 'names' => 'post_title', 'slugs' => 'post_name'); // select sql $fields = strtolower((string) $args['fields']); if (in_array($fields, array_keys($allowed_fields))) { $select_sql = "{$wpdb->posts}." . $allowed_fields[$fields]; } else { // not an allowed field - return full post objects $select_sql = "{$wpdb->posts}.*"; } // limit sql $limit_sql = ''; if (-1 !== (int) $args['limit_posts']) { $limit_posts = absint($args['limit_posts']); if ($limit_posts) { $limit_sql = 'LIMIT 0,' . $limit_posts; } } $orderby = strtolower((string) $args['orderby']); if (!in_array($orderby, array('post_date', 'post_modified'))) { $orderby = 'post_date'; } // limit date sql $limit_date_sql = ''; $limit_year = absint($args['limit_year']); $limit_month = absint($args['limit_month']); if ($limit_year || $limit_month) { // year takes precedence over month $time_limit = $limit_year ? $limit_year : $limit_month; $time_string = $limit_year ? 'year' : 'month'; $last_date = date('Y-m-t', strtotime("now")); $first_date = date('Y-m-d', strtotime("{$last_date} -{$time_limit} {$time_string}")); $limit_date_sql = " AND {$wpdb->posts}.{$orderby} > '{$first_date} 23:59:59' AND {$wpdb->posts}.{$orderby} <= '{$last_date} 23:59:59'"; $limit_sql = ''; // limit by date takes precedence over limit by posts } $order_by_sql = ''; $group_by_sql = "{$wpdb->posts}.ID"; if (!$order_by_rand) { if ($args['related']) { // sql for related terms count $select_sql .= " , count(distinct tt.term_taxonomy_id) as termcount"; } $order_by_sql = "{$wpdb->posts}.{$orderby}"; } // post thumbnail sql $meta_join_sql = $meta_where_sql = ''; if ($args['post_thumbnail']) { $meta_query = array(array('key' => '_thumbnail_id')); $meta = get_meta_sql($meta_query, 'post', $wpdb->posts, 'ID'); $meta_join_sql = isset($meta['join']) && $meta['join'] ? $meta['join'] : ''; $meta_where_sql = isset($meta['where']) && $meta['where'] ? $meta['where'] : ''; if ('' === $meta_join_sql || '' === $meta_join_sql) { $meta_join_sql = $meta_where_sql = ''; } } $pieces = array('select_sql', 'join_sql', 'where_sql', 'group_by_sql', 'order_by_sql', 'limit_sql'); /** * Filter the SELECT clause of the query. * * @since 0.3.1 * * @param string $select_sql The SELECT clause of the query. */ $select_sql = apply_filters_ref_array('related_posts_by_taxonomy_posts_fields', array($select_sql, $post_id, $taxonomies, $args)); $join_sql = "INNER JOIN {$wpdb->term_relationships} tr ON ({$wpdb->posts}.ID = tr.object_id)"; $join_sql .= " INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id){$meta_join_sql}"; /** * Filter the JOIN clause of the query. * * @since 0.3.1 * * @param string $join_sql The JOIN clause of the query. */ $join_sql = apply_filters_ref_array('related_posts_by_taxonomy_posts_join', array($join_sql, $post_id, $taxonomies, $args)); $where_sql = "{$where_sql} {$post_ids_sql}{$limit_date_sql} AND ( {$term_ids_sql} ){$meta_where_sql}"; /** * Filter the WHERE clause of the query. * * @since 0.3.1 * * @param string $where The WHERE clause of the query. */ $where_sql = apply_filters_ref_array('related_posts_by_taxonomy_posts_where', array($where_sql, $post_id, $taxonomies, $args)); /** * Filter the GROUP BY clause of the query. * * @since 0.3.1 * * @param string $groupby The GROUP BY clause of the query. */ $group_by_sql = apply_filters_ref_array('related_posts_by_taxonomy_posts_groupby', array($group_by_sql, $post_id, $taxonomies, $args)); $order_by_sql = "{$order_by_sql} {$order_sql}"; /** * Filter the ORDER BY clause of the query. * * @since 0.3.1 * * @param string $orderby The ORDER BY clause of the query. */ $order_by_sql = apply_filters_ref_array('related_posts_by_taxonomy_posts_orderby', array($order_by_sql, $post_id, $taxonomies, $args)); /** * Filter the LIMIT clause of the query. * * @since 0.3.1 * * @param string $limits The LIMIT clause of the query. */ $limit_sql = apply_filters_ref_array('related_posts_by_taxonomy_posts_limits', array($limit_sql, $post_id, $taxonomies, $args)); /** * Filter all query clauses at once, for convenience. * * Covers the WHERE, GROUP BY, JOIN, ORDER BY, * fields (SELECT), and LIMITS clauses. * * @since 0.3.1 * * @param array $pieces The list of clauses for the query. */ $clauses = (array) apply_filters_ref_array('related_posts_by_taxonomy_posts_clauses', array(compact($pieces), $post_id, $taxonomies, $args)); foreach ($pieces as $piece) { ${$piece} = isset($clauses[$piece]) ? $clauses[$piece] : ''; } if (!empty($group_by_sql)) { $group_by_sql = 'GROUP BY ' . $group_by_sql; } if (!empty($order_by_sql)) { $order_by_sql = 'ORDER BY ' . $order_by_sql; } $query = "SELECT {$select_sql} FROM {$wpdb->posts} {$join_sql} {$where_sql} {$group_by_sql} {$order_by_sql} {$limit_sql}"; $last_changed = wp_cache_get('last_changed', 'posts'); if (!$last_changed) { $last_changed = microtime(); wp_cache_set('last_changed', $last_changed, 'posts'); } $key = md5($query); $key = "get_related_taxonomy_posts:{$key}:{$last_changed}"; if (!($results = wp_cache_get($key, 'posts'))) { $results = $wpdb->get_results($query); wp_cache_set($key, $results, 'posts'); } if ($results) { if (!$order_by_rand && $args['related']) { /* add (termcount) score and key to results */ for ($i = 0; $i < count($results); $i++) { $results[$i]->score = array($results[$i]->termcount, $i); } /* order related posts */ uasort($results, 'km_rpbt_related_posts_by_taxonomy_cmp'); } $results = array_values($results); if (in_array($fields, array_keys($allowed_fields))) { $results = wp_list_pluck($results, $allowed_fields[$fields]); } if (-1 !== (int) $args['posts_per_page']) { $posts_per_page = absint($args['posts_per_page']); $posts_per_page = $posts_per_page ? $posts_per_page : 5; $results = array_slice($results, 0, $posts_per_page); } } else { $results = array(); } /** * Filter related_posts_by_taxonomy. * * @since 0.1 * * @param array $results Related posts. Array with Post objects or post IDs or post titles or post slugs. * @param int $post_id Post id used to get the related posts. * @param array $taxonomies Taxonomies used to get the related posts. * @param array $args Function arguments used to get the related posts. */ return apply_filters('related_posts_by_taxonomy', $results, $post_id, $taxonomies, $args); }
public function test_user_has_access_only_to_private_posts_for_certain_post_types() { register_post_type('foo', array('capabilities' => array('read_private_posts' => 'read_private_foo'))); register_post_type('bar', array('capabilities' => array('read_private_posts' => 'read_private_bar'))); register_post_type('baz', array('capabilities' => array('read_private_posts' => 'read_private_baz'))); $current_user = get_current_user_id(); $u = self::factory()->user->create(array('role' => 'editor')); $editor_role = get_role('editor'); $editor_role->add_cap('read_private_baz'); wp_set_current_user($u); $maybe_string = get_posts_by_author_sql(array('foo', 'bar', 'baz')); $editor_role->remove_cap('read_private_baz'); $this->assertNotContains("post_type = 'foo' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string); $this->assertNotContains("post_type = 'bar' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string); $this->assertContains("post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string); _unregister_post_type('foo'); _unregister_post_type('bar'); _unregister_post_type('baz'); wp_set_current_user($current_user); }
/** * Get the users based on sorting criteria and order * * Function identificator: fn0 * */ function select_sorting_order_criteria() { global $wpdb; $qv =& $this->query_vars; $this->fn0_query_results_array = array(); $this->fn0_query_fields = "wppb_t1.ID"; $this->fn0_query_from = "FROM {$wpdb->users} AS wppb_t1"; if ($qv['sorting_criteria'] == 'ID') { $criteria = 'wppb_t1.ID'; } elseif ($qv['sorting_criteria'] == 'login') { $criteria = 'wppb_t1.user_login'; } elseif ($qv['sorting_criteria'] == 'email') { $criteria = 'wppb_t1.user_email'; } elseif ($qv['sorting_criteria'] == 'url') { $criteria = 'wppb_t1.user_url'; } elseif ($qv['sorting_criteria'] == 'registered') { $criteria = 'wppb_t1.user_registered'; } elseif ($qv['sorting_criteria'] == 'nicename') { $criteria = 'wppb_t1.display_name'; } elseif ($qv['sorting_criteria'] == 'post_count') { $where = get_posts_by_author_sql('post'); $this->fn0_query_from .= " LEFT OUTER JOIN (SELECT post_author, COUNT(*) AS post_count FROM {$wpdb->posts} {$where} GROUP BY post_author) p ON wppb_t1.ID = p.post_author"; $criteria = 'wppb_t1.ID'; } elseif ($qv['sorting_criteria'] == 'bio') { $this->fn0_query_from .= " LEFT OUTER JOIN {$wpdb->usermeta} AS wppb_t2 ON wppb_t1.ID = wppb_t2.user_id AND wppb_t2.meta_key = 'description'"; $criteria = 'wppb_t2.meta_value'; } elseif ($qv['sorting_criteria'] == 'aim') { $this->fn0_query_from .= " LEFT OUTER JOIN {$wpdb->usermeta} AS wppb_t2 ON wppb_t1.ID = wppb_t2.user_id AND wppb_t2.meta_key = 'aim'"; $criteria = 'wppb_t2.meta_value'; } elseif ($qv['sorting_criteria'] == 'yim') { $this->fn0_query_from .= " LEFT OUTER JOIN {$wpdb->usermeta} AS wppb_t2 ON wppb_t1.ID = wppb_t2.user_id AND wppb_t2.meta_key = 'yim'"; $criteria = 'wppb_t2.meta_value'; } elseif ($qv['sorting_criteria'] == 'jabber') { $this->fn0_query_from .= " LEFT OUTER JOIN {$wpdb->usermeta} AS wppb_t2 ON wppb_t1.ID = wppb_t2.user_id AND wppb_t2.meta_key = 'jabber'"; $criteria = 'wppb_t2.meta_value'; } elseif ($qv['sorting_criteria'] == 'firstname') { $this->fn0_query_from .= " LEFT OUTER JOIN {$wpdb->usermeta} AS wppb_t2 ON wppb_t1.ID = wppb_t2.user_id AND wppb_t2.meta_key = 'first_name'"; $criteria = 'wppb_t2.meta_value'; } elseif ($qv['sorting_criteria'] == 'lastname') { $this->fn0_query_from .= " LEFT OUTER JOIN {$wpdb->usermeta} AS wppb_t2 ON wppb_t1.ID = wppb_t2.user_id AND wppb_t2.meta_key = 'last_name'"; $criteria = 'wppb_t2.meta_value'; } else { $wppbFetchArray = get_option('wppb_custom_fields', 'not_found'); if ($wppbFetchArray != 'not_found') { foreach ($wppbFetchArray as $thisKey => $thisValue) { if ($thisValue['item_type'] != 'heading') { if ($qv['sorting_criteria'] == $thisValue['item_metaName']) { $this->fn0_query_from .= " LEFT OUTER JOIN {$wpdb->usermeta} AS wppb_t2 ON wppb_t1.ID = wppb_t2.user_id AND wppb_t2.meta_key = '" . $thisValue['item_metaName'] . "'"; $criteria = 'wppb_t2.meta_value'; } } } } } $this->fn0_query_where = "WHERE 1"; $this->fn0_query_orderby = "ORDER BY {$criteria} " . strtoupper(trim($qv['sorting_order'])); $this->fn0_query_limit = ""; do_action_ref_array('wppb_pre_select_by_sorting_order_and_criteria', array(&$this)); $this->fn0_query_results = apply_filters('wppb_select_by_sorting_order_and_criteria_result', $wpdb->get_results(trim("SELECT {$this->fn0_query_fields} {$this->fn0_query_from} {$this->fn0_query_where} {$this->fn0_query_orderby} {$this->fn0_query_limit}"))); //create an array with IDs from result foreach ($this->fn0_query_results as $qr_key => $qr_value) { array_push($this->fn0_query_results_array, $qr_value->ID); } $this->fn0_query_results_array = apply_filters('wppb_select_by_sorting_order_and_criteria_array', $this->fn0_query_results_array); do_action_ref_array('wppb_post_select_by_sorting_order_and_criteria', array(&$this)); }
/** * Number of posts written by a list of users. * * @since 3.0.0 * @param array $userid User ID number list. * @return array Amount of posts each user has written. */ function count_many_users_posts($users) { global $wpdb; $count = array(); if (!is_array($users) || empty($users)) { return $count; } $userlist = implode(',', $users); $where = get_posts_by_author_sql('post'); $result = $wpdb->get_results("SELECT post_author, COUNT(*) FROM {$wpdb->posts} {$where} AND post_author IN ({$userlist}) GROUP BY post_author", ARRAY_N); foreach ($result as $row) { $count[$row[0]] = $row[1]; } foreach ($users as $id) { if (!isset($count[$id])) { $count[$id] = 0; } } return $count; }
function count_user_posts_by_type($userid, $post_type = 'post') { global $wpdb; $where = get_posts_by_author_sql($post_type, true, $userid); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); return $count; }
function count_user_posts_by_type($userid, $post_type = 'post', $public_only = false) { global $wpdb; $where = get_posts_by_author_sql($post_type, true, $userid, $public_only); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE ( ( post_type = 'quote' AND ( post_status = 'publish' ) ) ) AND post_author = 684"); return $where; //apply_filters( 'get_usernumposts', $count, $userid ); }
function marketify_count_user_downloads($userid, $post_type = 'download') { if (false === ($count = get_transient($userid . $post_type))) { global $wpdb; $where = get_posts_by_author_sql($post_type, true, $userid); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); set_transient($userid . $post_type, $count, 12 * HOUR_IN_SECONDS); } return apply_filters('get_usernumposts', $count, $userid); }
/** * Return the raw database count of replies by a user * * @since bbPress (r3633) * @global WPDB $wpdb * @uses bbp_get_user_id() * @uses get_posts_by_author_sql() * @uses bbp_get_reply_post_type() * @uses apply_filters() * @return int Raw DB count of replies */ function bbp_get_user_reply_count_raw($user_id = 0) { $user_id = bbp_get_user_id($user_id); if (empty($user_id)) { return false; } global $wpdb; $where = get_posts_by_author_sql(bbp_get_reply_post_type(), true, $user_id); $count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); return (int) apply_filters('bbp_get_user_reply_count_raw', $count, $user_id); }
public function test_administrator_should_have_access_to_private_posts_when_public_only_is_false(){ $current_user = get_current_user_id(); $u = $this->factory->user->create( array( 'role' => 'administrator' ) ); wp_set_current_user( $u ); $maybe_string = get_posts_by_author_sql( 'post', true, null, false ); $this->assertContains( "post_status = 'private'", $maybe_string ); $this->assertNotContains( 'post_author', $maybe_string ); wp_set_current_user( $current_user ); }
function getItemByReviewer($reviewer) { $where = get_posts_by_author_sql($this->post_type, true, $reviewer); return array_shift($this->dbo->get_col("SELECT * FROM {$this->dbo->posts} {$where}")); }
function search_result($args) { global $wpdb, $wp_version, $upme_options; if (!isset($this->search_form_name)) { $this->search_form_name = ''; } $this->search_query = array(); $this->search_user_argument = array(); $search_user_status_filters = array(); // Set search args when search is not used in the list page if (!isset($this->search_args)) { $this->search_args = array(); $this->search_args['operator'] = 'AND'; } // Insert additional inner join to order the results by a custom meta field $orderby_meta_join = ''; $orderby_meta_default_where = ''; if ($this->profile_orderby_custom_status || 'yes' == $this->profile_orderby_custom_status) { $orderby_meta_join = ' INNER JOIN ' . $wpdb->usermeta . ' as mta ON (users.ID = mta.user_id) '; $orderby_meta_default_where = ' (mta.meta_key = "' . $this->profile_order_field . '" ) '; } // End /* Customize orderby_meta_join to include addtional joins */ $orderby_meta_custom_joins = ''; $search_custom_orderby_meta_joins_params = array(); $orderby_meta_custom_joins = apply_filters('upme_search_custom_orderby_meta_joins', $orderby_meta_custom_joins, $search_custom_orderby_meta_joins_params); $orderby_meta_custom_where = apply_filters('upme_search_custom_orderby_meta_where', array(), $search_custom_orderby_meta_joins_params); foreach ($orderby_meta_custom_where as $order_where) { array_push($search_user_status_filters, $order_where); } $this->search_query_string = "SELECT DISTINCT users.* FROM " . $wpdb->users . " as users "; $this->count_search_query_string = "SELECT COUNT(DISTINCT(users.ID)) FROM " . $wpdb->users . " as users"; $current_option = $upme_options->upme_settings; // Only consider active users for results $this->search_query_search_param = array(); if (!current_user_can('manage_options') && !current_user_can('manage_upme_options')) { // When profile_view_status is not enabled, all users will be active /* UPME Filters for enable/disable profile status on search */ $search_profile_status_params = array('form_name' => $this->search_form_name); $search_profile_status = apply_filters('upme_search_profile_status', true, $search_profile_status_params); // End Filter if ($search_profile_status) { array_push($search_user_status_filters, "(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%upme_user_profile_status::ACTIVE%')"); } } if (!current_user_can('manage_options') && !current_user_can('manage_upme_options')) { /* UPME Filters for enable/disable profile status on search */ $search_approval_status_params = array('form_name' => $this->search_form_name); $search_approval_status = apply_filters('upme_search_approval_status', true, $search_approval_status_params); // End Filter if ($search_approval_status) { array_push($search_user_status_filters, "(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%upme_approval_status::ACTIVE%')"); } /* UPME Filters for enable/disable profile status on search */ $search_activation_status_params = array('form_name' => $this->search_form_name); $search_activation_status = apply_filters('upme_search_activation_status', true, $search_activation_status_params); // End Filter if ($search_activation_status) { array_push($search_user_status_filters, "(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%upme_activation_status::ACTIVE%')"); } } if ('' != $orderby_meta_default_where) { array_push($search_user_status_filters, $orderby_meta_default_where); } // Apply mandatory filters on search results $this->search_query_custom_param = apply_filters('upme_search_user_status', $search_user_status_filters); $this->combined_search_query_search_param = array(); if (upme_is_post()) { if (upme_is_in_post('upme_combined_search') && upme_is_in_post('upme_combined_search_fields')) { if (upme_post_value('upme_combined_search_fields') != '' && upme_post_value('upme_combined_search') != '') { $fields = explode(',', upme_post_value('upme_combined_search_fields')); if (version_compare($wp_version, '4.0', '>=')) { $escaped_value = $wpdb->esc_like(upme_post_value('upme_combined_search')); } else { $escaped_value = like_escape(upme_post_value('upme_combined_search')); } $combined_search_text = esc_sql($escaped_value); foreach ($fields as $key => $value) { $this->combined_search_query_search_param[] = "(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%" . $value . '::%' . $combined_search_text . "%')"; } $this->search_query_search_param[] = '( ' . implode(' OR ', $this->combined_search_query_search_param) . ' )'; } } if (upme_is_in_post('upme_search')) { foreach ($_POST['upme_search'] as $key => $value) { $process = false; if (is_array($value) && count($value) > 0) { $process = true; } else { if ($value != '' && $value != '0') { $process = true; } else { $process = false; } } if ($process === true) { if (is_array($value)) { foreach ($value as $k => $v) { $this->search_query_search_param[] = "(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%" . $key . '::%' . esc_sql(trim($v)) . "%')"; } } else { $this->search_query_search_param[] = "(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%" . $key . '::%' . esc_sql($value) . "%')"; } } } } } $this->search_query_role_param = ''; if ($this->profile_role) { $this->profile_role = explode(',', $this->profile_role); if (count($this->profile_role) > 0) { foreach ($this->profile_role as $key => $value) { $this->search_query_role_param[] = "(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%role::" . $value . "%')"; } } $role_operator = 'AND'; $this->search_query_role_param = ' ' . $role_operator . ' ( ' . implode(' OR ', $this->search_query_role_param) . ' ) '; } // Hiding Admins from the list when attribute is set if (!current_user_can('manage_options') && !current_user_can('manage_upme_options') && $this->hide_admin_role && ($this->hide_admin_role == 'yes' || $this->hide_admin_role == 'true')) { $search_query_role_param[] = "(mt.meta_key = '_upme_search_cache' AND mt.meta_value NOT LIKE '%role::administrator%')"; $role_operator = 'AND'; $this->search_query_role_param .= ' ' . $role_operator . ' ( ' . implode(' OR ', $search_query_role_param) . ' ) '; } // Setting up order data, This is required before adding search conditions $post_count_sort = ''; if (in_array($this->profile_order_field, array('nicename', 'email', 'url', 'registered'))) { $orderby = 'users.user_' . $this->profile_order_field; } elseif (in_array($this->profile_order_field, array('user_nicename', 'user_email', 'user_url', 'user_registered'))) { $orderby = 'users.' . $this->profile_order_field; } elseif ('name' == $this->profile_order_field || 'display_name' == $this->profile_order_field) { $orderby = 'users.display_name'; } elseif ('ID' == $this->profile_order_field || 'id' == $this->profile_order_field) { $orderby = 'users.ID'; } else { if ('post_count' == $this->profile_order_field) { $where = get_posts_by_author_sql('post'); $post_count_sort = " LEFT OUTER JOIN (\r\n SELECT post_author, COUNT(*) as post_count\r\n FROM {$wpdb->posts}\r\n {$where}\r\n GROUP BY post_author\r\n ) p ON (users.ID = p.post_author)\r\n "; $orderby = 'post_count'; } else { if ($this->profile_orderby_custom_status || 'yes' == $this->profile_orderby_custom_status) { $orderby = 'mta.meta_value'; } else { $orderby = 'users.user_login'; } } } // Add custom query conditions into the query $search_query_custom_params = ''; if (count($this->search_query_custom_param) > 0) { $search_query_custom_params = ' AND ' . implode(' AND ', $this->search_query_custom_param); } if (count($this->search_query_search_param) > 0) { // Results after searching - POST variables are available $this->search_query_string .= ' INNER JOIN ' . $wpdb->usermeta . ' as mt ON (users.ID = mt.user_id) ' . $orderby_meta_join . ' ' . $orderby_meta_custom_joins . ' ' . $post_count_sort . ' WHERE 1=1 AND (' . implode(' ' . $this->search_args['operator'] . ' ', $this->search_query_search_param) . ') ' . $search_query_custom_params; $this->count_search_query_string .= ' INNER JOIN ' . $wpdb->usermeta . ' as mt ON (users.ID = mt.user_id) ' . $orderby_meta_join . ' ' . $orderby_meta_custom_joins . ' ' . $post_count_sort . ' WHERE 1=1 AND (' . implode(' ' . $this->search_args['operator'] . ' ', $this->search_query_search_param) . ') ' . $search_query_custom_params; // Search users with provided cusotm field and provided custom value if ($this->upme_args['group_meta'] != '' && $this->upme_args['group_meta_value'] != '') { $this->search_query_string .= " AND (mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%" . $this->upme_args['group_meta'] . "::" . $this->upme_args['group_meta_value'] . "%')"; $this->count_search_query_string .= " AND (mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%" . $this->upme_args['group_meta'] . "::" . $this->upme_args['group_meta_value'] . "%')"; } } else { // Default results before searching - POST variables not available if ($this->upme_args['group_meta'] != '' && $this->upme_args['group_meta_value'] != '') { // Search users with provided cusotm field and provided custom value $this->search_query_string .= " INNER JOIN " . $wpdb->usermeta . " as mt ON (users.ID = mt.user_id) " . $orderby_meta_join . " " . $orderby_meta_custom_joins . " " . $post_count_sort . " WHERE 1=1 AND (mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%" . $this->upme_args['group_meta'] . "::" . $this->upme_args['group_meta_value'] . "%')" . $search_query_custom_params; $this->count_search_query_string .= " INNER JOIN " . $wpdb->usermeta . " as mt ON (users.ID = mt.user_id) " . $orderby_meta_join . " " . $orderby_meta_custom_joins . " " . $post_count_sort . " WHERE 1=1 AND (mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%" . $this->upme_args['group_meta'] . "::" . $this->upme_args['group_meta_value'] . "%')" . $search_query_custom_params; } else { $default_join_status = false; if ($this->search_query_role_param != '' || count($this->search_query_custom_param) > 0) { $default_join_status = true; } // Set default join state when role status is available or custom params are available // without default search params $default_inner_join = ''; if ($default_join_status) { $default_inner_join = ' INNER JOIN ' . $wpdb->usermeta . ' as mt ON (users.ID = mt.user_id) '; } $this->search_query_string .= $post_count_sort . $default_inner_join . $orderby_meta_join . $orderby_meta_custom_joins . $search_query_custom_params; $this->count_search_query_string .= $post_count_sort . $default_inner_join . $orderby_meta_join . $orderby_meta_custom_joins . $search_query_custom_params; } } if ($this->search_query_role_param != '') { $this->search_query_string .= $this->search_query_role_param; $this->count_search_query_string .= $this->search_query_role_param; } /* Custom orderby fields through filters */ $orderby = apply_filters('upme_search_custom_orderby_conditions', $orderby, $search_custom_orderby_meta_joins_params); $this->search_query_string .= ' ORDER BY ' . $orderby . ' ' . $this->profile_order; $this->count_search_query_string .= ' ORDER BY ' . $orderby . ' ' . $this->profile_order; // Execute custom actions on search result users do_action('upme_search_result_export', $this->search_query_string); // Setting Limit Data if ('-1' == $args['per_page']) { $this->search_query_string .= ' '; } else { if (isset($this->current_users_page) && $this->current_users_page > 1) { $offset = ($this->current_users_page - 1) * $args['per_page']; $this->search_query_string .= ' LIMIT ' . $offset . ',' . $args['per_page']; } else { $this->search_query_string .= ' LIMIT ' . $args['per_page']; } } $this->count_search_query_string .= ' LIMIT 1'; $this->total_matching_user = $wpdb->get_var($this->count_search_query_string); $this->searched_users = $wpdb->get_results($this->search_query_string); if ('yes' == strtolower($args['show_random'])) { shuffle($this->searched_users); if ($args['result_range_start'] && $args['result_range_count'] && $args['result_range_start'] > 0 && $args['result_range_count'] > 0) { $this->searched_users = array_slice($this->searched_users, (int) $args['result_range_start'] - 1, $args['result_range_count']); } } }
/** * Get idea authors sorted by count * * count_many_users_posts() does not match the need * * @package WP Idea Stream * @subpackage users/functions * * @since 2.0.0 * * @global $wpdb * @param int $max the number of users to limit the query * @uses get_posts_by_author_sql() to get the sql part for the author request * @uses wp_idea_stream_get_post_type() to get the ideas post type identifier * @return array list of users ordered by ideas count. */ function wp_idea_stream_users_ideas_count_by_user($max = 10) { global $wpdb; $sql = array(); $sql['select'] = "SELECT p.post_author, COUNT(p.ID) as count_ideas, u.user_nicename"; $sql['from'] = "FROM {$wpdb->posts} p LEFT JOIN {$wpdb->users} u ON ( p.post_author = u.ID )"; $sql['where'] = get_posts_by_author_sql(wp_idea_stream_get_post_type(), true, null, true); $sql['groupby'] = 'GROUP BY p.post_author'; $sql['order'] = 'ORDER BY count_ideas DESC'; $sql['limit'] = $wpdb->prepare('LIMIT 0, %d', $max); $query = apply_filters('wp_idea_stream_users_ideas_count_by_user_query', join(' ', $sql), $sql, $max); return $wpdb->get_results($query); }
/** * count_future_events function * * @return Count future events **/ public function count_future_events($user_id = null) { if (is_admin()) { $settings = $this->_registry->get('model.settings'); $current_time = $this->_registry->get('date.time'); $current_time->set_timezone($settings->get('timezone_string')); $current_time = $current_time->format_to_gmt(); $user_id = get_current_user_id(); $where = get_posts_by_author_sql(AI1EC_POST_TYPE, true, $user_id); $db = $this->_registry->get('dbi.dbi'); $posts = $db->get_table_name('posts'); $table_name = $db->get_table_name('ai1ec_events'); $sql = "SELECT COUNT(*) FROM {$table_name} INNER JOIN {$posts} on {$table_name}.post_id = {$posts}.ID" . " {$where} AND {$table_name}.start > {$current_time}"; //future event return $db->get_var($sql); } else { return 0; } }
function wpsight_count_user_posts_by_type($userid, $post_type = 'post') { global $wpdb; // Author SQL $where = get_posts_by_author_sql($post_type, TRUE, $userid); // Set SQL query $query = "SELECT COUNT(*) FROM {$wpdb->posts} {$where}"; // Get count var $count = $wpdb->get_var($query); return apply_filters('get_usernumposts', $count, $userid); }
/** * Prepare the query variables. * * @since 3.1.0 * * @param string|array $args Optional. The query variables. */ function prepare_query($query = array()) { global $wpdb; if (empty($this->query_vars) || !empty($query)) { $this->query_limit = null; $this->query_vars = wp_parse_args($query, array('blog_id' => $GLOBALS['blog_id'], 'role' => '', 'meta_key' => '', 'meta_value' => '', 'meta_compare' => '', 'include' => array(), 'exclude' => array(), 'search' => '', 'search_columns' => array(), 'orderby' => 'login', 'order' => 'ASC', 'offset' => '', 'number' => '', 'count_total' => true, 'fields' => 'all', 'who' => '')); } $qv =& $this->query_vars; if (is_array($qv['fields'])) { $qv['fields'] = array_unique($qv['fields']); $this->query_fields = array(); foreach ($qv['fields'] as $field) { $field = 'ID' === $field ? 'ID' : sanitize_key($field); $this->query_fields[] = "{$wpdb->users}.{$field}"; } $this->query_fields = implode(',', $this->query_fields); } elseif ('all' == $qv['fields']) { $this->query_fields = "{$wpdb->users}.*"; } else { $this->query_fields = "{$wpdb->users}.ID"; } if (isset($qv['count_total']) && $qv['count_total']) { $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; } $this->query_from = "FROM {$wpdb->users}"; $this->query_where = "WHERE 1=1"; // sorting if (isset($qv['orderby'])) { if (in_array($qv['orderby'], array('nicename', 'email', 'url', 'registered'))) { $orderby = 'user_' . $qv['orderby']; } elseif (in_array($qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered'))) { $orderby = $qv['orderby']; } elseif ('name' == $qv['orderby'] || 'display_name' == $qv['orderby']) { $orderby = 'display_name'; } elseif ('post_count' == $qv['orderby']) { // todo: avoid the JOIN $where = get_posts_by_author_sql('post'); $this->query_from .= " LEFT OUTER JOIN (\r\n\t\t\t\t\tSELECT post_author, COUNT(*) as post_count\r\n\t\t\t\t\tFROM {$wpdb->posts}\r\n\t\t\t\t\t{$where}\r\n\t\t\t\t\tGROUP BY post_author\r\n\t\t\t\t) p ON ({$wpdb->users}.ID = p.post_author)\r\n\t\t\t\t"; $orderby = 'post_count'; } elseif ('ID' == $qv['orderby'] || 'id' == $qv['orderby']) { $orderby = 'ID'; } elseif ('meta_value' == $qv['orderby']) { $orderby = "{$wpdb->usermeta}.meta_value"; } else { $orderby = 'user_login'; } } if (empty($orderby)) { $orderby = 'user_login'; } $qv['order'] = isset($qv['order']) ? strtoupper($qv['order']) : ''; if ('ASC' == $qv['order']) { $order = 'ASC'; } else { $order = 'DESC'; } $this->query_orderby = "ORDER BY {$orderby} {$order}"; // limit if (isset($qv['number']) && $qv['number']) { if ($qv['offset']) { $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); } else { $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']); } } $search = ''; if (isset($qv['search'])) { $search = trim($qv['search']); } if ($search) { $leading_wild = ltrim($search, '*') != $search; $trailing_wild = rtrim($search, '*') != $search; if ($leading_wild && $trailing_wild) { $wild = 'both'; } elseif ($leading_wild) { $wild = 'leading'; } elseif ($trailing_wild) { $wild = 'trailing'; } else { $wild = false; } if ($wild) { $search = trim($search, '*'); } $search_columns = array(); if ($qv['search_columns']) { $search_columns = array_intersect($qv['search_columns'], array('ID', 'user_login', 'user_email', 'user_url', 'user_nicename')); } if (!$search_columns) { if (false !== strpos($search, '@')) { $search_columns = array('user_email'); } elseif (is_numeric($search)) { $search_columns = array('user_login', 'ID'); } elseif (preg_match('|^https?://|', $search) && !(is_multisite() && wp_is_large_network('users'))) { $search_columns = array('user_url'); } else { $search_columns = array('user_login', 'user_nicename'); } } /** * Filter the columns to search in a WP_User_Query search. * * The default columns depend on the search term, and include 'user_email', * 'user_login', 'ID', 'user_url', and 'user_nicename'. * * @since 3.6.0 * * @param array $search_columns Array of column names to be searched. * @param string $search Text being searched. * @param WP_User_Query $this The current WP_User_Query instance. */ $search_columns = apply_filters('user_search_columns', $search_columns, $search, $this); $this->query_where .= $this->get_search_sql($search, $search_columns, $wild); } $blog_id = 0; if (isset($qv['blog_id'])) { $blog_id = absint($qv['blog_id']); } if (isset($qv['who']) && 'authors' == $qv['who'] && $blog_id) { $qv['meta_key'] = $wpdb->get_blog_prefix($blog_id) . 'user_level'; $qv['meta_value'] = 0; $qv['meta_compare'] = '!='; $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query } $role = ''; if (isset($qv['role'])) { $role = trim($qv['role']); } if ($blog_id && ($role || is_multisite())) { $cap_meta_query = array(); $cap_meta_query['key'] = $wpdb->get_blog_prefix($blog_id) . 'capabilities'; if ($role) { $cap_meta_query['value'] = '"' . $role . '"'; $cap_meta_query['compare'] = 'like'; } if (empty($qv['meta_query']) || !in_array($cap_meta_query, $qv['meta_query'], true)) { $qv['meta_query'][] = $cap_meta_query; } } $meta_query = new WP_Meta_Query(); $meta_query->parse_query_vars($qv); if (!empty($meta_query->queries)) { $clauses = $meta_query->get_sql('user', $wpdb->users, 'ID', $this); $this->query_from .= $clauses['join']; $this->query_where .= $clauses['where']; if ('OR' == $meta_query->relation) { $this->query_fields = 'DISTINCT ' . $this->query_fields; } } if (!empty($qv['include'])) { $ids = implode(',', wp_parse_id_list($qv['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})"; } /** * 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 $this The current WP_User_Query instance, * passed by reference. */ do_action_ref_array('pre_user_query', array(&$this)); }
function mars_get_user_postcount($user_id, $post_type = "video") { global $wpdb; $where = get_posts_by_author_sql($post_type, true, $user_id); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); return $count; }
/** * Prepare the query variables * * @since 3.1.0 * @access private */ function prepare_query() { global $wpdb; $qv =& $this->query_vars; if (is_array($qv['fields'])) { $qv['fields'] = array_unique($qv['fields']); $this->query_fields = array(); foreach ($qv['fields'] as $field) { $this->query_fields[] = $wpdb->users . '.' . esc_sql($field); } $this->query_fields = implode(',', $this->query_fields); } elseif ('all' == $qv['fields']) { $this->query_fields = "{$wpdb->users}.*"; } else { $this->query_fields = "{$wpdb->users}.ID"; } if ($this->query_vars['count_total']) { $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; } $this->query_from = "FROM {$wpdb->users}"; $this->query_where = "WHERE 1=1"; // sorting if (in_array($qv['orderby'], array('nicename', 'email', 'url', 'registered'))) { $orderby = 'user_' . $qv['orderby']; } elseif (in_array($qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered'))) { $orderby = $qv['orderby']; } elseif ('name' == $qv['orderby'] || 'display_name' == $qv['orderby']) { $orderby = 'display_name'; } elseif ('post_count' == $qv['orderby']) { // todo: avoid the JOIN $where = get_posts_by_author_sql('post'); $this->query_from .= " LEFT OUTER JOIN (\n\t\t\t\tSELECT post_author, COUNT(*) as post_count\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t{$where}\n\t\t\t\tGROUP BY post_author\n\t\t\t) p ON ({$wpdb->users}.ID = p.post_author)\n\t\t\t"; $orderby = 'post_count'; } elseif ('ID' == $qv['orderby'] || 'id' == $qv['orderby']) { $orderby = 'ID'; } else { $orderby = 'user_login'; } $qv['order'] = strtoupper($qv['order']); if ('ASC' == $qv['order']) { $order = 'ASC'; } else { $order = 'DESC'; } $this->query_orderby = "ORDER BY {$orderby} {$order}"; // limit if ($qv['number']) { if ($qv['offset']) { $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); } else { $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']); } } $search = trim($qv['search']); if ($search) { $leading_wild = ltrim($search, '*') != $search; $trailing_wild = rtrim($search, '*') != $search; if ($leading_wild && $trailing_wild) { $wild = 'both'; } elseif ($leading_wild) { $wild = 'leading'; } elseif ($trailing_wild) { $wild = 'trailing'; } else { $wild = false; } if ($wild) { $search = trim($search, '*'); } if (false !== strpos($search, '@')) { $search_columns = array('user_email'); } elseif (is_numeric($search)) { $search_columns = array('user_login', 'ID'); } elseif (preg_match('|^https?://|', $search)) { $search_columns = array('user_url'); } else { $search_columns = array('user_login', 'user_nicename'); } $this->query_where .= $this->get_search_sql($search, $search_columns, $wild); } $blog_id = absint($qv['blog_id']); if ('authors' == $qv['who'] && $blog_id) { $qv['meta_key'] = $wpdb->get_blog_prefix($blog_id) . 'user_level'; $qv['meta_value'] = 0; $qv['meta_compare'] = '!='; $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query } $role = trim($qv['role']); if ($blog_id && ($role || is_multisite())) { $cap_meta_query = array(); $cap_meta_query['key'] = $wpdb->get_blog_prefix($blog_id) . 'capabilities'; if ($role) { $cap_meta_query['value'] = '"' . $role . '"'; $cap_meta_query['compare'] = 'like'; } $qv['meta_query'][] = $cap_meta_query; } $meta_query = new WP_Meta_Query(); $meta_query->parse_query_vars($qv); if (!empty($meta_query->queries)) { $clauses = $meta_query->get_sql('user', $wpdb->users, 'ID', $this); $this->query_from .= $clauses['join']; $this->query_where .= $clauses['where']; if ('OR' == $meta_query->relation) { $this->query_fields = 'DISTINCT ' . $this->query_fields; } } if (!empty($qv['include'])) { $ids = implode(',', wp_parse_id_list($qv['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})"; } do_action_ref_array('pre_user_query', array(&$this)); }
echo '<div class="author-info url"><i class="fa fa-link icon-link"></i><a href="' . $author_data->user_url . '" target="_blank" >' . $author_data->user_url . '</a></div>'; } echo '</div>'; // author-info if (!empty($author_meta['social-network'][0])) { echo '<div class="gdlr-lms-author-social">' . do_shortcode($author_meta['social-network'][0]) . '</div>'; } // count the post by this author and display link global $wpdb; $where = get_posts_by_author_sql('course', true, $author_id); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); if ($count) { echo '<a class="gdlr-lms-button cyan" href="' . esc_url(add_query_arg('post_type', 'course')); echo '" >' . __('View courses by', 'gdlr-lms') . ' ' . $author_meta['first_name'][0] . '</a>'; } else { $where = get_posts_by_author_sql('post', true, $author_id); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); if ($count) { echo '<a class="gdlr-lms-button cyan" href="' . esc_url(add_query_arg('post_type', 'post')); echo '" >' . __('View posts by', 'gdlr-lms') . ' ' . $author_meta['first_name'][0] . '</a>'; } } echo '</div>'; // author-info-wrapper // extra info echo '<div class="gdlr-lms-author-content-wrapper">'; echo '<div class="gdlr-lms-author-extra-info-wrapper">'; $extra_infos = array('location' => __('Location', 'gdlr-lms'), 'current-work' => __('Current Work', 'gdlr-lms'), 'past-work' => __('Past Work', 'gdlr-lms'), 'specialist' => __('Specialist In', 'gdlr-lms'), 'experience' => __('Experience', 'gdlr-lms')); foreach ($extra_infos as $key => $value) { if (!empty($author_meta[$key][0])) { echo '<div class="gdlr-lms-extra-info ' . $key . '" >';
/** * Number of posts written by a list of users. * * @since 3.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $users Array of user IDs. * @param string|array $post_type Optional. Single post type or array of post types to check. Defaults to 'post'. * @param bool $public_only Optional. Only return counts for public posts. Defaults to false. * @return array Amount of posts each user has written. */ function count_many_users_posts($users, $post_type = 'post', $public_only = false) { global $wpdb; $count = array(); if (empty($users) || !is_array($users)) { return $count; } $userlist = implode(',', array_map('absint', $users)); $where = get_posts_by_author_sql($post_type, true, null, $public_only); $result = $wpdb->get_results("SELECT post_author, COUNT(*) FROM {$wpdb->posts} {$where} AND post_author IN ({$userlist}) GROUP BY post_author", ARRAY_N); foreach ($result as $row) { $count[$row[0]] = $row[1]; } foreach ($users as $id) { if (!isset($count[$id])) { $count[$id] = 0; } } return $count; }
function dwqa_user_answer_number($user_id) { global $wpdb; $where = get_posts_by_author_sql('dwqa-answer', true, $user_id, true); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); return apply_filters('get_usernumposts', $count, $user_id); }
/** * Retrieve the private post SQL based on capability. * * This function provides a standardized way to appropriately select on the * post_status of posts/pages. The function will return a piece of SQL code that * can be added to a WHERE clause; this SQL is constructed to allow all * published posts, and all private posts to which the user has access. * * It also allows plugins that define their own post type to control the cap by * using the hook 'pub_priv_sql_capability'. The plugin is expected to return * the capability the user must have to read the private post type. * * @since 2.2.0 * * @uses $user_ID * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types. * * @param string $post_type currently only supports 'post' or 'page'. * @return string SQL code that can be added to a where clause. */ function get_private_posts_cap_sql($post_type) { return get_posts_by_author_sql($post_type, FALSE); }
/** * Prepare the query variables * * @since 3.1.0 * @access private */ function prepare_query() { global $wpdb; $qv =& $this->query_vars; $this->query_from = " FROM {$wpdb->users}"; $this->query_where = " WHERE 1=1"; // sorting if (in_array($qv['orderby'], array('email', 'url', 'registered'))) { $orderby = 'user_' . $qv['orderby']; } elseif ('name' == $qv['orderby']) { $orderby = 'display_name'; } elseif ('post_count' == $qv['orderby']) { $where = get_posts_by_author_sql('post'); $this->query_from .= " LEFT OUTER JOIN (\n\t\t\t\tSELECT post_author, COUNT(*) as post_count\n\t\t\t\tFROM wp_posts\n\t\t\t\t{$where}\n\t\t\t\tGROUP BY post_author\n\t\t\t) p ON (wp_users.ID = p.post_author)\n\t\t\t"; $orderby = 'post_count'; } else { $orderby = 'user_login'; } $qv['order'] = strtoupper($qv['order']); if ('ASC' == $qv['order']) { $order = 'ASC'; } else { $order = 'DESC'; } $this->query_orderby = " ORDER BY {$orderby} {$order}"; // limit if ($qv['number']) { if ($qv['offset']) { $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $qv['offset'], $qv['offset'] + $qv['number']); } else { $this->query_limit = $wpdb->prepare(" LIMIT %d", $qv['number']); } } $search = trim($qv['search']); if ($search) { $this->query_where .= _wp_search_sql($search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name')); } $role = trim($qv['role']); if ($role) { $this->query_from .= " INNER JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb->usermeta}.user_id"; $this->query_where .= $wpdb->prepare(" AND {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}capabilities' AND {$wpdb->usermeta}.meta_value LIKE %s", '%' . $role . '%'); } elseif (is_multisite()) { $level_key = $wpdb->prefix . 'capabilities'; // wpmu site admins don't have user_levels $this->query_from .= ", {$wpdb->usermeta}"; $this->query_where .= " AND {$wpdb->users}.ID = {$wpdb->usermeta}.user_id AND meta_key = '{$level_key}'"; } $meta_key = trim($qv['meta_key']); $meta_value = trim($qv['meta_value']); if ($meta_key) { if (empty($meta_value)) { $subquery = $wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key); } else { $subquery = $wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value); } $this->query_where .= " AND {$wpdb->users}.ID IN ({$subquery})"; } if (!empty($qv['include'])) { $ids = implode(',', wp_parse_id_list($qv['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})"; } do_action_ref_array('pre_user_query', array(&$this)); }