Example #1
0
function c2c_get_recent_custom($field, $before = '', $after = '', $none = '', $between = ', ', $before_last = '', $limit = 1, $unique = false, $order = 'DESC', $include_static = true, $show_pass_post = false)
{
    global $wpdb;
    if (empty($between)) {
        $limit = 1;
    }
    if ($order != 'ASC') {
        $order = 'DESC';
    }
    $now = current_time('mysql');
    $sql = "SELECT ";
    if ($unique) {
        $sql .= "DISTINCT ";
    }
    $sql .= "meta_value FROM {$wpdb->posts} AS posts, {$wpdb->postmeta} AS postmeta ";
    $sql .= "WHERE posts.ID = postmeta.post_id AND postmeta.meta_key = '{$field}' ";
    $sql .= "AND ( posts.post_status = 'publish' ";
    if ($include_static) {
        $sql .= " OR posts.post_status = 'static' ";
    }
    $sql .= " ) AND posts.post_date < '{$now}' ";
    if (!$show_pass_post) {
        $sql .= "AND posts.post_password = '' ";
    }
    $sql .= "AND postmeta.meta_value != '' ";
    $sql .= "ORDER BY posts.post_date {$order} LIMIT {$limit}";
    $results = array();
    $values = array();
    $results = $wpdb->get_results($sql);
    if (!empty($results)) {
        foreach ($results as $result) {
            $values[] = $result->meta_value;
        }
    }
    return c2c__format_custom($field, $values, $before, $after, $none, $between, $before_last);
}
Example #2
0
 /**
  * Template tag for use outside "the loop" and applies for custom fields regardless of post
  *
  * @param string $field The name/key of the custom field
  * @param string $before (optional) The text to display before all the custom field value(s), if any are present (defaults to '')
  * @param string $after (optional) The text to display after all the custom field value(s), if any are present (defaults to '')
  * @param string $none (optional) The text to display in place of the field value should no field values exist; if defined as '' and no field value exists, then nothing (including no `$before` and `$after`) gets displayed.
  * @param string $between (optional) The text to display between multiple occurrences of the custom field; if defined as '', then only the first instance will be used.
  * @param string $before_last (optional) The text to display between the next-to-last and last items listed when multiple occurrences of the custom field; `$between` MUST be set to something other than '' for this to take effect.
  * @param int $limit (optional) The limit to the number of custom fields to retrieve. Use 0 to indicate no limit.
  * @param bool $unique (optional) Boolean ('true' or 'false') to indicate if each custom field value in the results should be unique (defaults to "false")
  * @param string $order (optional) Indicates if the results should be sorted in chronological order ('ASC') (the earliest custom field value listed first), or reverse chronological order ('DESC') (the most recent custom field value listed first).
  * @param bool $include_pages (optional) Boolean ('true' or 'false') to indicate if pages should be included when retrieving recent custom values; default is 'true'
  * @param bool $show_pass_post (optional) Boolean ('true' or 'false') to indicate if password protected posts should be included when retrieving recent custom values; default is 'false'
  * @return string The formatted string
  */
 function c2c_get_recent_custom($field, $before = '', $after = '', $none = '', $between = ', ', $before_last = '', $limit = 1, $unique = false, $order = 'DESC', $include_pages = true, $show_pass_post = false)
 {
     global $wpdb;
     $limit = intval($limit);
     if (empty($between)) {
         $limit = 1;
     }
     if ($order != 'ASC') {
         $order = 'DESC';
     }
     $sql = "SELECT ";
     if ($unique) {
         $sql .= "DISTINCT ";
     }
     $sql .= "meta_value FROM {$wpdb->posts} AS posts, {$wpdb->postmeta} AS postmeta ";
     $sql .= "WHERE posts.ID = postmeta.post_id AND postmeta.meta_key = %s ";
     $sql .= "AND posts.post_status = 'publish' AND ( posts.post_type = 'post' ";
     if ($include_pages) {
         $sql .= "OR posts.post_type = 'page' ";
     }
     $sql .= ') ';
     if (!$show_pass_post) {
         $sql .= "AND posts.post_password = '' ";
     }
     $sql .= "AND postmeta.meta_value != '' ";
     $sql .= "ORDER BY posts.post_date {$order}";
     //die("((".$limit."))");
     if ($limit > 0) {
         $sql .= ' LIMIT %d';
     }
     $results = array();
     $values = array();
     $results = $wpdb->get_results($wpdb->prepare($sql, $field, $limit));
     if (!empty($results)) {
         foreach ($results as $result) {
             $values[] = $result->meta_value;
         }
     }
     return c2c__format_custom($field, $values, $before, $after, $none, $between, $before_last);
 }