[ Index ]

WordPress Source Cross Reference

title

Body

[close]

/wp-includes/ -> post.php (source)

   1  <?php
   2  
   3  //
   4  // Post functions
   5  //
   6  
   7  function get_attached_file($attachment_id) {
   8      return get_post_meta($attachment_id, '_wp_attached_file', true);
   9  }
  10  
  11  function &get_children($post = 0, $output = OBJECT) {
  12      global $post_cache, $wpdb;
  13  
  14      if ( empty($post) ) {
  15          if ( isset($GLOBALS['post']) )
  16              $post_parent = & $GLOBALS['post']->post_parent;
  17          else
  18              return false;
  19      } elseif ( is_object($post) ) {
  20          $post_parent = $post->post_parent;
  21      } else {
  22          $post_parent = $post;
  23      }
  24  
  25      $post_parent = (int) $post_parent;
  26  
  27      $query = "SELECT * FROM $wpdb->posts WHERE post_parent = $post_parent";
  28  
  29      $children = $wpdb->get_results($query);
  30  
  31      if ( $children ) {
  32          foreach ( $children as $key => $child ) {
  33              $post_cache[$child->ID] =& $children[$key];
  34              $kids[$child->ID] =& $children[$key];
  35          }
  36      } else {
  37          return false;
  38      }
  39  
  40      if ( $output == OBJECT ) {
  41          return $kids;
  42      } elseif ( $output == ARRAY_A ) {
  43          foreach ( $kids as $kid )
  44              $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
  45          return $weeuns;
  46      } elseif ( $output == ARRAY_N ) {
  47          foreach ( $kids as $kid )
  48              $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
  49          return $babes;
  50      } else {
  51          return $kids;
  52      }
  53  }
  54  
  55  // get extended entry info (<!--more-->)
  56  function get_extended($post) {
  57      list($main,$extended) = explode('<!--more-->', $post, 2);
  58  
  59      // Strip leading and trailing whitespace
  60      $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
  61      $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
  62  
  63      return array('main' => $main, 'extended' => $extended);
  64  }
  65  
  66  // Retrieves post data given a post ID or post object.
  67  // Handles post caching.
  68  function &get_post(&$post, $output = OBJECT) {
  69      global $post_cache, $wpdb;
  70  
  71      if ( empty($post) ) {
  72          if ( isset($GLOBALS['post']) )
  73              $_post = & $GLOBALS['post'];
  74          else
  75              $_post = null;
  76      } elseif ( is_object($post) ) {
  77          if ( 'page' == $post->post_type )
  78              return get_page($post, $output);
  79          if ( !isset($post_cache[$post->ID]) )
  80              $post_cache[$post->ID] = &$post;
  81          $_post = & $post_cache[$post->ID];
  82      } else {
  83          if ( $_post = wp_cache_get($post, 'pages') )
  84              return get_page($_post, $output);
  85          elseif ( isset($post_cache[$post]) )
  86              $_post = & $post_cache[$post];
  87          else {
  88              $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
  89              $_post = & $wpdb->get_row($query);
  90              if ( 'page' == $_post->post_type )
  91                  return get_page($_post, $output);
  92              $post_cache[$post] = & $_post;
  93          }
  94      }
  95  
  96      if ( defined(WP_IMPORTING) )
  97          unset($post_cache);
  98  
  99      if ( $output == OBJECT ) {
 100          return $_post;
 101      } elseif ( $output == ARRAY_A ) {
 102          return get_object_vars($_post);
 103      } elseif ( $output == ARRAY_N ) {
 104          return array_values(get_object_vars($_post));
 105      } else {
 106          return $_post;
 107      }
 108  }
 109  
 110  // Takes a post ID, returns its mime type.
 111  function get_post_mime_type($ID = '') {
 112      $post = & get_post($ID);
 113  
 114      if ( is_object($post) )
 115          return $post->post_mime_type;
 116  
 117      return false;
 118  }
 119  
 120  function get_post_status($ID = '') {
 121      $post = get_post($ID);
 122  
 123      if ( is_object($post) ) {
 124          if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
 125              return get_post_status($post->post_parent);
 126          else
 127              return $post->post_status;
 128      }
 129  
 130      return false;
 131  }
 132  
 133  function get_post_type($post = false) {
 134      global $wpdb, $posts;
 135  
 136      if ( false === $post )
 137          $post = $posts[0];
 138      elseif ( (int) $post )
 139          $post = get_post($post, OBJECT);
 140  
 141      if ( is_object($post) )
 142          return $post->post_type;
 143  
 144      return false;
 145  }
 146  
 147  function get_posts($args) {
 148      global $wpdb;
 149  
 150      if ( is_array($args) )
 151          $r = &$args;
 152      else
 153          parse_str($args, $r);
 154  
 155      $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => '',
 156          'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' =>'');
 157      $r = array_merge($defaults, $r);
 158      extract($r);
 159  
 160      $inclusions = '';
 161      if ( !empty($include) ) {
 162          $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value params if using include
 163          $category = ''; 
 164          $exclude = '';  
 165          $meta_key = '';
 166          $meta_value = '';
 167          $incposts = preg_split('/[\s,]+/',$include);
 168          $numberposts = count($incposts);  // only the number of posts included
 169          if ( count($incposts) ) {
 170              foreach ( $incposts as $incpost ) {
 171                  if (empty($inclusions))
 172                      $inclusions = ' AND ( ID = ' . intval($incpost) . ' ';
 173                  else
 174                      $inclusions .= ' OR ID = ' . intval($incpost) . ' ';
 175              }
 176          }
 177      }
 178      if (!empty($inclusions)) 
 179          $inclusions .= ')';    
 180  
 181      $exclusions = '';
 182      if ( !empty($exclude) ) {
 183          $exposts = preg_split('/[\s,]+/',$exclude);
 184          if ( count($exposts) ) {
 185              foreach ( $exposts as $expost ) {
 186                  if (empty($exclusions))
 187                      $exclusions = ' AND ( ID <> ' . intval($expost) . ' ';
 188                  else
 189                      $exclusions .= ' AND ID <> ' . intval($expost) . ' ';
 190              }
 191          }
 192      }
 193      if (!empty($exclusions)) 
 194          $exclusions .= ')';
 195  
 196      $query ="SELECT DISTINCT * FROM $wpdb->posts " ;
 197      $query .= ( empty( $category ) ? "" : ", $wpdb->post2cat " ) ; 
 198      $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ; 
 199      $query .= " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " ;
 200      $query .= ( empty( $category ) ? "" : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") " ) ;
 201      $query .= ( empty( $meta_key ) | empty($meta_value)  ? "" : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )" ) ;
 202      $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts ;
 203  
 204      $posts = $wpdb->get_results($query);
 205  
 206      update_post_caches($posts);
 207  
 208      return $posts;
 209  }
 210  
 211  //
 212  // Post meta functions
 213  //
 214  
 215  function add_post_meta($post_id, $key, $value, $unique = false) {
 216      global $wpdb, $post_meta_cache;
 217  
 218      if ( $unique ) {
 219          if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
 220  = '$key' AND post_id = '$post_id'") ) {
 221              return false;
 222          }
 223      }
 224  
 225      $original = $value;
 226      if ( is_array($value) || is_object($value) )
 227          $value = $wpdb->escape(serialize($value));
 228  
 229      $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
 230  
 231      $post_meta_cache['$post_id'][$key][] = $original;
 232  
 233      return true;
 234  }
 235  
 236  function delete_post_meta($post_id, $key, $value = '') {
 237      global $wpdb, $post_meta_cache;
 238  
 239      if ( empty($value) ) {
 240          $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
 241  post_id = '$post_id' AND meta_key = '$key'");
 242      } else {
 243          $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
 244  post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
 245      }
 246  
 247      if ( !$meta_id )
 248          return false;
 249  
 250      if ( empty($value) ) {
 251          $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
 252  AND meta_key = '$key'");
 253          unset($post_meta_cache['$post_id'][$key]);
 254      } else {
 255          $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
 256  AND meta_key = '$key' AND meta_value = '$value'");
 257          $cache_key = $post_meta_cache['$post_id'][$key];
 258          if ($cache_key) foreach ( $cache_key as $index => $data )
 259              if ( $data == $value )
 260                  unset($post_meta_cache['$post_id'][$key][$index]);
 261      }
 262  
 263      unset($post_meta_cache['$post_id'][$key]);
 264  
 265      return true;
 266  }
 267  
 268  function get_post_meta($post_id, $key, $single = false) {
 269      global $wpdb, $post_meta_cache;
 270  
 271      if ( isset($post_meta_cache[$post_id][$key]) ) {
 272          if ( $single ) {
 273              return maybe_unserialize( $post_meta_cache[$post_id][$key][0] );
 274          } else {
 275              return maybe_unserialize( $post_meta_cache[$post_id][$key] );
 276          }
 277      }
 278  
 279      $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
 280  
 281      $values = array();
 282      if ( $metalist ) {
 283          foreach ($metalist as $metarow) {
 284              $values[] = $metarow[0];
 285          }
 286      }
 287  
 288      if ( $single ) {
 289          if ( count($values) ) {
 290              $return = maybe_unserialize( $values[0] );
 291          } else {
 292              return '';
 293          }
 294      } else {
 295          $return = $values;
 296      }
 297  
 298      return maybe_unserialize($return);
 299  }
 300  
 301  function update_post_meta($post_id, $key, $value, $prev_value = '') {
 302      global $wpdb, $post_meta_cache;
 303  
 304      $original_value = $value;
 305      if ( is_array($value) || is_object($value) )
 306          $value = $wpdb->escape(serialize($value));
 307  
 308      $original_prev = $prev_value;
 309      if ( is_array($prev_value) || is_object($prev_value) )
 310          $prev_value = $wpdb->escape(serialize($prev_value));
 311  
 312      if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
 313  = '$key' AND post_id = '$post_id'") ) {
 314          return false;
 315      }
 316  
 317      if ( empty($prev_value) ) {
 318          $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
 319  meta_key = '$key' AND post_id = '$post_id'");
 320          $cache_key = $post_meta_cache['$post_id'][$key];
 321          if ( !empty($cache_key) )
 322              foreach ($cache_key as $index => $data)
 323                  $post_meta_cache['$post_id'][$key][$index] = $original_value;
 324      } else {
 325          $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
 326  meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
 327          $cache_key = $post_meta_cache['$post_id'][$key];
 328          if ( !empty($cache_key) )
 329              foreach ($cache_key as $index => $data)
 330                  if ( $data == $original_prev )
 331                      $post_meta_cache['$post_id'][$key][$index] = $original_value;
 332      }
 333  
 334      return true;
 335  }
 336  
 337  
 338  function get_post_custom( $post_id = 0 ) {
 339      global $id, $post_meta_cache, $wpdb;
 340  
 341      if ( ! $post_id )
 342          $post_id = $id;
 343  
 344      if ( isset($post_meta_cache[$post_id]) )
 345          return $post_meta_cache[$post_id];
 346  
 347      if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta    WHERE post_id = '$post_id' ORDER BY post_id, meta_key", ARRAY_A) ) {
 348          // Change from flat structure to hierarchical:
 349          $post_meta_cache = array();
 350          foreach ( $meta_list as $metarow ) {
 351              $mpid = $metarow['post_id'];
 352              $mkey = $metarow['meta_key'];
 353              $mval = $metarow['meta_value'];
 354  
 355              // Force subkeys to be array type:
 356              if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) )
 357                  $post_meta_cache[$mpid] = array();
 358  
 359              if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) )
 360                  $post_meta_cache[$mpid]["$mkey"] = array();
 361  
 362              // Add a value to the current pid/key:
 363              $post_meta_cache[$mpid][$mkey][] = $mval;
 364          }
 365          return $post_meta_cache[$mpid];
 366      }
 367  }
 368  
 369  function get_post_custom_keys( $post_id = 0 ) {
 370      $custom = get_post_custom( $post_id );
 371  
 372      if ( ! is_array($custom) )
 373          return;
 374  
 375      if ( $keys = array_keys($custom) )
 376          return $keys;
 377  }
 378  
 379  
 380  function get_post_custom_values( $key = '', $post_id = 0 ) {
 381      $custom = get_post_custom($post_id);
 382  
 383      return $custom[$key];
 384  }
 385  
 386  function wp_delete_post($postid = 0) {
 387      global $wpdb, $wp_rewrite;
 388      $postid = (int) $postid;
 389  
 390      if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
 391          return $post;
 392  
 393      if ( 'attachment' == $post->post_type )
 394          return wp_delete_attachment($postid);
 395  
 396      do_action('delete_post', $postid);
 397  
 398      if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
 399          $categories = wp_get_post_categories($post->ID);
 400          if( is_array( $categories ) ) {
 401              foreach ( $categories as $cat_id ) {
 402                  $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
 403                  wp_cache_delete($cat_id, 'category');
 404              }
 405          }
 406      }
 407  
 408      if ( 'page' == $post->post_type )
 409          $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'");
 410  
 411