PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/post.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 1419 lines | 608 code | 168 blank | 643 comment | 133 complexity | b47522ff889950d9ebb5d8f5a508193f MD5 | raw file
  1. <?php
  2. /**
  3. * Post functions and post utility function.
  4. *
  5. * @package WordPress
  6. * @subpackage Post
  7. * @since 1.5.0
  8. */
  9. //
  10. // Post Type Registration
  11. //
  12. /**
  13. * Creates the initial post types when 'init' action is fired.
  14. */
  15. function create_initial_post_types() {
  16. register_post_type( 'post', array(
  17. 'public' => true,
  18. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  19. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  20. 'capability_type' => 'post',
  21. 'hierarchical' => false,
  22. 'rewrite' => false,
  23. 'query_var' => false,
  24. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
  25. ) );
  26. register_post_type( 'page', array(
  27. 'public' => true,
  28. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  29. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  30. 'capability_type' => 'page',
  31. 'hierarchical' => true,
  32. 'rewrite' => false,
  33. 'query_var' => false,
  34. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  35. ) );
  36. register_post_type( 'attachment', array(
  37. 'labels' => array(
  38. 'name' => __( 'Media' ),
  39. ),
  40. 'public' => true,
  41. 'show_ui' => false,
  42. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  43. '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
  44. 'capability_type' => 'post',
  45. 'hierarchical' => false,
  46. 'rewrite' => false,
  47. 'query_var' => false,
  48. 'can_export' => false,
  49. 'show_in_nav_menus' => false,
  50. ) );
  51. register_post_type( 'revision', array(
  52. 'labels' => array(
  53. 'name' => __( 'Revisions' ),
  54. 'singular_name' => __( 'Revision' ),
  55. ),
  56. 'public' => false,
  57. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  58. '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
  59. 'capability_type' => 'post',
  60. 'hierarchical' => false,
  61. 'rewrite' => false,
  62. 'query_var' => false,
  63. ) );
  64. register_post_type( 'nav_menu_item', array(
  65. 'labels' => array(
  66. 'name' => __( 'Navigation Menu Items' ),
  67. 'singular_name' => __( 'Navigation Menu Item' ),
  68. ),
  69. 'public' => false,
  70. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  71. 'capability_type' => 'post',
  72. 'hierarchical' => false,
  73. 'rewrite' => false,
  74. 'query_var' => false,
  75. ) );
  76. register_post_status( 'publish', array(
  77. 'label' => _x( 'Published', 'post' ),
  78. 'public' => true,
  79. '_builtin' => true, /* internal use only. */
  80. 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
  81. ) );
  82. register_post_status( 'future', array(
  83. 'label' => _x( 'Scheduled', 'post' ),
  84. 'protected' => true,
  85. '_builtin' => true, /* internal use only. */
  86. 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
  87. ) );
  88. register_post_status( 'draft', array(
  89. 'label' => _x( 'Draft', 'post' ),
  90. 'protected' => true,
  91. '_builtin' => true, /* internal use only. */
  92. 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
  93. ) );
  94. register_post_status( 'pending', array(
  95. 'label' => _x( 'Pending', 'post' ),
  96. 'protected' => true,
  97. '_builtin' => true, /* internal use only. */
  98. 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
  99. ) );
  100. register_post_status( 'private', array(
  101. 'label' => _x( 'Private', 'post' ),
  102. 'private' => true,
  103. '_builtin' => true, /* internal use only. */
  104. 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
  105. ) );
  106. register_post_status( 'trash', array(
  107. 'label' => _x( 'Trash', 'post' ),
  108. 'internal' => true,
  109. '_builtin' => true, /* internal use only. */
  110. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
  111. 'show_in_admin_status_list' => true,
  112. ) );
  113. register_post_status( 'auto-draft', array(
  114. 'label' => 'auto-draft',
  115. 'internal' => true,
  116. '_builtin' => true, /* internal use only. */
  117. ) );
  118. register_post_status( 'inherit', array(
  119. 'label' => 'inherit',
  120. 'internal' => true,
  121. '_builtin' => true, /* internal use only. */
  122. 'exclude_from_search' => false,
  123. ) );
  124. }
  125. add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
  126. /**
  127. * Retrieve attached file path based on attachment ID.
  128. *
  129. * You can optionally send it through the 'get_attached_file' filter, but by
  130. * default it will just return the file path unfiltered.
  131. *
  132. * The function works by getting the single post meta name, named
  133. * '_wp_attached_file' and returning it. This is a convenience function to
  134. * prevent looking up the meta name and provide a mechanism for sending the
  135. * attached filename through a filter.
  136. *
  137. * @since 2.0.0
  138. * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID.
  139. *
  140. * @param int $attachment_id Attachment ID.
  141. * @param bool $unfiltered Whether to apply filters.
  142. * @return string The file path to the attached file.
  143. */
  144. function get_attached_file( $attachment_id, $unfiltered = false ) {
  145. $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
  146. // If the file is relative, prepend upload dir
  147. if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
  148. $file = $uploads['basedir'] . "/$file";
  149. if ( $unfiltered )
  150. return $file;
  151. return apply_filters( 'get_attached_file', $file, $attachment_id );
  152. }
  153. /**
  154. * Update attachment file path based on attachment ID.
  155. *
  156. * Used to update the file path of the attachment, which uses post meta name
  157. * '_wp_attached_file' to store the path of the attachment.
  158. *
  159. * @since 2.1.0
  160. * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
  161. *
  162. * @param int $attachment_id Attachment ID
  163. * @param string $file File path for the attachment
  164. * @return bool False on failure, true on success.
  165. */
  166. function update_attached_file( $attachment_id, $file ) {
  167. if ( !get_post( $attachment_id ) )
  168. return false;
  169. $file = apply_filters( 'update_attached_file', $file, $attachment_id );
  170. $file = _wp_relative_upload_path($file);
  171. return update_post_meta( $attachment_id, '_wp_attached_file', $file );
  172. }
  173. /**
  174. * Return relative path to an uploaded file.
  175. *
  176. * The path is relative to the current upload dir.
  177. *
  178. * @since 2.9.0
  179. * @uses apply_filters() Calls '_wp_relative_upload_path' on file path.
  180. *
  181. * @param string $path Full path to the file
  182. * @return string relative path on success, unchanged path on failure.
  183. */
  184. function _wp_relative_upload_path( $path ) {
  185. $new_path = $path;
  186. if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
  187. if ( 0 === strpos($new_path, $uploads['basedir']) ) {
  188. $new_path = str_replace($uploads['basedir'], '', $new_path);
  189. $new_path = ltrim($new_path, '/');
  190. }
  191. }
  192. return apply_filters( '_wp_relative_upload_path', $new_path, $path );
  193. }
  194. /**
  195. * Retrieve all children of the post parent ID.
  196. *
  197. * Normally, without any enhancements, the children would apply to pages. In the
  198. * context of the inner workings of WordPress, pages, posts, and attachments
  199. * share the same table, so therefore the functionality could apply to any one
  200. * of them. It is then noted that while this function does not work on posts, it
  201. * does not mean that it won't work on posts. It is recommended that you know
  202. * what context you wish to retrieve the children of.
  203. *
  204. * Attachments may also be made the child of a post, so if that is an accurate
  205. * statement (which needs to be verified), it would then be possible to get
  206. * all of the attachments for a post. Attachments have since changed since
  207. * version 2.5, so this is most likely unaccurate, but serves generally as an
  208. * example of what is possible.
  209. *
  210. * The arguments listed as defaults are for this function and also of the
  211. * {@link get_posts()} function. The arguments are combined with the
  212. * get_children defaults and are then passed to the {@link get_posts()}
  213. * function, which accepts additional arguments. You can replace the defaults in
  214. * this function, listed below and the additional arguments listed in the
  215. * {@link get_posts()} function.
  216. *
  217. * The 'post_parent' is the most important argument and important attention
  218. * needs to be paid to the $args parameter. If you pass either an object or an
  219. * integer (number), then just the 'post_parent' is grabbed and everything else
  220. * is lost. If you don't specify any arguments, then it is assumed that you are
  221. * in The Loop and the post parent will be grabbed for from the current post.
  222. *
  223. * The 'post_parent' argument is the ID to get the children. The 'numberposts'
  224. * is the amount of posts to retrieve that has a default of '-1', which is
  225. * used to get all of the posts. Giving a number higher than 0 will only
  226. * retrieve that amount of posts.
  227. *
  228. * The 'post_type' and 'post_status' arguments can be used to choose what
  229. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
  230. * post types are 'post', 'pages', and 'attachments'. The 'post_status'
  231. * argument will accept any post status within the write administration panels.
  232. *
  233. * @see get_posts() Has additional arguments that can be replaced.
  234. * @internal Claims made in the long description might be inaccurate.
  235. *
  236. * @since 2.0.0
  237. *
  238. * @param mixed $args Optional. User defined arguments for replacing the defaults.
  239. * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
  240. * @return array|bool False on failure and the type will be determined by $output parameter.
  241. */
  242. function &get_children($args = '', $output = OBJECT) {
  243. $kids = array();
  244. if ( empty( $args ) ) {
  245. if ( isset( $GLOBALS['post'] ) ) {
  246. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
  247. } else {
  248. return $kids;
  249. }
  250. } elseif ( is_object( $args ) ) {
  251. $args = array('post_parent' => (int) $args->post_parent );
  252. } elseif ( is_numeric( $args ) ) {
  253. $args = array('post_parent' => (int) $args);
  254. }
  255. $defaults = array(
  256. 'numberposts' => -1, 'post_type' => 'any',
  257. 'post_status' => 'any', 'post_parent' => 0,
  258. );
  259. $r = wp_parse_args( $args, $defaults );
  260. $children = get_posts( $r );
  261. if ( !$children )
  262. return $kids;
  263. update_post_cache($children);
  264. foreach ( $children as $key => $child )
  265. $kids[$child->ID] =& $children[$key];
  266. if ( $output == OBJECT ) {
  267. return $kids;
  268. } elseif ( $output == ARRAY_A ) {
  269. foreach ( (array) $kids as $kid )
  270. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
  271. return $weeuns;
  272. } elseif ( $output == ARRAY_N ) {
  273. foreach ( (array) $kids as $kid )
  274. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
  275. return $babes;
  276. } else {
  277. return $kids;
  278. }
  279. }
  280. /**
  281. * Get extended entry info (<!--more-->).
  282. *
  283. * There should not be any space after the second dash and before the word
  284. * 'more'. There can be text or space(s) after the word 'more', but won't be
  285. * referenced.
  286. *
  287. * The returned array has 'main' and 'extended' keys. Main has the text before
  288. * the <code><!--more--></code>. The 'extended' key has the content after the
  289. * <code><!--more--></code> comment.
  290. *
  291. * @since 1.0.0
  292. *
  293. * @param string $post Post content.
  294. * @return array Post before ('main') and after ('extended').
  295. */
  296. function get_extended($post) {
  297. //Match the new style more links
  298. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
  299. list($main, $extended) = explode($matches[0], $post, 2);
  300. } else {
  301. $main = $post;
  302. $extended = '';
  303. }
  304. // Strip leading and trailing whitespace
  305. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
  306. $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
  307. return array('main' => $main, 'extended' => $extended);
  308. }
  309. /**
  310. * Retrieves post data given a post ID or post object.
  311. *
  312. * See {@link sanitize_post()} for optional $filter values. Also, the parameter
  313. * $post, must be given as a variable, since it is passed by reference.
  314. *
  315. * @since 1.5.1
  316. * @uses $wpdb
  317. * @link http://codex.wordpress.org/Function_Reference/get_post
  318. *
  319. * @param int|object $post Post ID or post object.
  320. * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  321. * @param string $filter Optional, default is raw.
  322. * @return mixed Post data
  323. */
  324. function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
  325. global $wpdb;
  326. $null = null;
  327. if ( empty($post) ) {
  328. if ( isset($GLOBALS['post']) )
  329. $_post = & $GLOBALS['post'];
  330. else
  331. return $null;
  332. } elseif ( is_object($post) && empty($post->filter) ) {
  333. _get_post_ancestors($post);
  334. $_post = sanitize_post($post, 'raw');
  335. wp_cache_add($post->ID, $_post, 'posts');
  336. } else {
  337. if ( is_object($post) )
  338. $post = $post->ID;
  339. $post = (int) $post;
  340. if ( ! $_post = wp_cache_get($post, 'posts') ) {
  341. $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
  342. if ( ! $_post )
  343. return $null;
  344. _get_post_ancestors($_post);
  345. $_post = sanitize_post($_post, 'raw');
  346. wp_cache_add($_post->ID, $_post, 'posts');
  347. }
  348. }
  349. if ($filter != 'raw')
  350. $_post = sanitize_post($_post, $filter);
  351. if ( $output == OBJECT ) {
  352. return $_post;
  353. } elseif ( $output == ARRAY_A ) {
  354. $__post = get_object_vars($_post);
  355. return $__post;
  356. } elseif ( $output == ARRAY_N ) {
  357. $__post = array_values(get_object_vars($_post));
  358. return $__post;
  359. } else {
  360. return $_post;
  361. }
  362. }
  363. /**
  364. * Retrieve ancestors of a post.
  365. *
  366. * @since 2.5.0
  367. *
  368. * @param int|object $post Post ID or post object
  369. * @return array Ancestor IDs or empty array if none are found.
  370. */
  371. function get_post_ancestors($post) {
  372. $post = get_post($post);
  373. if ( !empty($post->ancestors) )
  374. return $post->ancestors;
  375. return array();
  376. }
  377. /**
  378. * Retrieve data from a post field based on Post ID.
  379. *
  380. * Examples of the post field will be, 'post_type', 'post_status', 'content',
  381. * etc and based off of the post object property or key names.
  382. *
  383. * The context values are based off of the taxonomy filter functions and
  384. * supported values are found within those functions.
  385. *
  386. * @since 2.3.0
  387. * @uses sanitize_post_field() See for possible $context values.
  388. *
  389. * @param string $field Post field name
  390. * @param id $post Post ID
  391. * @param string $context Optional. How to filter the field. Default is display.
  392. * @return WP_Error|string Value in post field or WP_Error on failure
  393. */
  394. function get_post_field( $field, $post, $context = 'display' ) {
  395. $post = (int) $post;
  396. $post = get_post( $post );
  397. if ( is_wp_error($post) )
  398. return $post;
  399. if ( !is_object($post) )
  400. return '';
  401. if ( !isset($post->$field) )
  402. return '';
  403. return sanitize_post_field($field, $post->$field, $post->ID, $context);
  404. }
  405. /**
  406. * Retrieve the mime type of an attachment based on the ID.
  407. *
  408. * This function can be used with any post type, but it makes more sense with
  409. * attachments.
  410. *
  411. * @since 2.0.0
  412. *
  413. * @param int $ID Optional. Post ID.
  414. * @return bool|string False on failure or returns the mime type
  415. */
  416. function get_post_mime_type($ID = '') {
  417. $post = & get_post($ID);
  418. if ( is_object($post) )
  419. return $post->post_mime_type;
  420. return false;
  421. }
  422. /**
  423. * Retrieve the post status based on the Post ID.
  424. *
  425. * If the post ID is of an attachment, then the parent post status will be given
  426. * instead.
  427. *
  428. * @since 2.0.0
  429. *
  430. * @param int $ID Post ID
  431. * @return string|bool Post status or false on failure.
  432. */
  433. function get_post_status($ID = '') {
  434. $post = get_post($ID);
  435. if ( !is_object($post) )
  436. return false;
  437. // Unattached attachments are assumed to be published.
  438. if ( ('attachment' == $post->post_type) && ('inherit' == $post->post_status) && ( 0 == $post->post_parent) )
  439. return 'publish';
  440. if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
  441. return get_post_status($post->post_parent);
  442. return $post->post_status;
  443. }
  444. /**
  445. * Retrieve all of the WordPress supported post statuses.
  446. *
  447. * Posts have a limited set of valid status values, this provides the
  448. * post_status values and descriptions.
  449. *
  450. * @since 2.5.0
  451. *
  452. * @return array List of post statuses.
  453. */
  454. function get_post_statuses( ) {
  455. $status = array(
  456. 'draft' => __('Draft'),
  457. 'pending' => __('Pending Review'),
  458. 'private' => __('Private'),
  459. 'publish' => __('Published')
  460. );
  461. return $status;
  462. }
  463. /**
  464. * Retrieve all of the WordPress support page statuses.
  465. *
  466. * Pages have a limited set of valid status values, this provides the
  467. * post_status values and descriptions.
  468. *
  469. * @since 2.5.0
  470. *
  471. * @return array List of page statuses.
  472. */
  473. function get_page_statuses( ) {
  474. $status = array(
  475. 'draft' => __('Draft'),
  476. 'private' => __('Private'),
  477. 'publish' => __('Published')
  478. );
  479. return $status;
  480. }
  481. /**
  482. * Register a post type. Do not use before init.
  483. *
  484. * A simple function for creating or modifying a post status based on the
  485. * parameters given. The function will accept an array (second optional
  486. * parameter), along with a string for the post status name.
  487. *
  488. *
  489. * Optional $args contents:
  490. *
  491. * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
  492. * public - Whether posts of this status should be shown in the admin UI. Defaults to true.
  493. * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to true.
  494. *
  495. * @package WordPress
  496. * @subpackage Post
  497. * @since 3.0.0
  498. * @uses $wp_post_statuses Inserts new post status object into the list
  499. *
  500. * @param string $post_status Name of the post status.
  501. * @param array|string $args See above description.
  502. */
  503. function register_post_status($post_status, $args = array()) {
  504. global $wp_post_statuses;
  505. if (!is_array($wp_post_statuses))
  506. $wp_post_statuses = array();
  507. // Args prefixed with an underscore are reserved for internal use.
  508. $defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null);
  509. $args = wp_parse_args($args, $defaults);
  510. $args = (object) $args;
  511. $post_status = sanitize_user($post_status, true);
  512. $args->name = $post_status;
  513. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  514. $args->internal = true;
  515. if ( null === $args->public )
  516. $args->public = false;
  517. if ( null === $args->private )
  518. $args->private = false;
  519. if ( null === $args->protected )
  520. $args->protected = false;
  521. if ( null === $args->internal )
  522. $args->internal = false;
  523. if ( null === $args->publicly_queryable )
  524. $args->publicly_queryable = $args->public;
  525. if ( null === $args->exclude_from_search )
  526. $args->exclude_from_search = $args->internal;
  527. if ( null === $args->show_in_admin_all_list )
  528. $args->show_in_admin_all_list = !$args->internal;
  529. if ( null === $args->show_in_admin_status_list )
  530. $args->show_in_admin_status_list = !$args->internal;
  531. if ( null === $args->single_view_cap )
  532. $args->single_view_cap = $args->public ? '' : 'edit';
  533. if ( false === $args->label )
  534. $args->label = $post_status;
  535. if ( false === $args->label_count )
  536. $args->label_count = array( $args->label, $args->label );
  537. $wp_post_statuses[$post_status] = $args;
  538. return $args;
  539. }
  540. /**
  541. * Retrieve a post status object by name
  542. *
  543. * @package WordPress
  544. * @subpackage Post
  545. * @since 3.0.0
  546. * @uses $wp_post_statuses
  547. * @see register_post_status
  548. * @see get_post_statuses
  549. *
  550. * @param string $post_type The name of a registered post status
  551. * @return object A post status object
  552. */
  553. function get_post_status_object( $post_status ) {
  554. global $wp_post_statuses;
  555. if ( empty($wp_post_statuses[$post_status]) )
  556. return null;
  557. return $wp_post_statuses[$post_status];
  558. }
  559. /**
  560. * Get a list of all registered post status objects.
  561. *
  562. * @package WordPress
  563. * @subpackage Post
  564. * @since 3.0.0
  565. * @uses $wp_post_statuses
  566. * @see register_post_status
  567. * @see get_post_status_object
  568. *
  569. * @param array|string $args An array of key => value arguments to match against the post status objects.
  570. * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
  571. * @param string $operator The logical operation to perform. 'or' means only one element
  572. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  573. * @return array A list of post type names or objects
  574. */
  575. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  576. global $wp_post_statuses;
  577. $field = ('names' == $output) ? 'name' : false;
  578. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  579. }
  580. /**
  581. * Whether the post type is hierarchical.
  582. *
  583. * A false return value might also mean that the post type does not exist.
  584. *
  585. * @since 3.0.0
  586. * @see get_post_type_object
  587. *
  588. * @param string $post Post type name
  589. * @return bool Whether post type is hierarchical.
  590. */
  591. function is_post_type_hierarchical( $post_type ) {
  592. if ( ! is_post_type( $post_type ) )
  593. return false;
  594. $post_type = get_post_type_object( $post_type );
  595. return $post_type->hierarchical;
  596. }
  597. /**
  598. * Checks if a post type is registered.
  599. *
  600. * @since 3.0.0
  601. * @uses get_post_type()
  602. *
  603. * @param string Post type name
  604. * @return bool Whether post type is registered.
  605. */
  606. function is_post_type( $post_type ) {
  607. return (bool) get_post_type_object( $post_type );
  608. }
  609. /**
  610. * Retrieve the post type of the current post or of a given post.
  611. *
  612. * @since 2.1.0
  613. *
  614. * @uses $post The Loop current post global
  615. *
  616. * @param mixed $the_post Optional. Post object or post ID.
  617. * @return bool|string post type or false on failure.
  618. */
  619. function get_post_type( $the_post = false ) {
  620. global $post;
  621. if ( false === $the_post )
  622. $the_post = $post;
  623. elseif ( is_numeric($the_post) )
  624. $the_post = get_post($the_post);
  625. if ( is_object($the_post) )
  626. return $the_post->post_type;
  627. return false;
  628. }
  629. /**
  630. * Retrieve a post type object by name
  631. *
  632. * @package WordPress
  633. * @subpackage Post
  634. * @since 3.0.0
  635. * @uses $wp_post_types
  636. * @see register_post_type
  637. * @see get_post_types
  638. *
  639. * @param string $post_type The name of a registered post type
  640. * @return object A post type object
  641. */
  642. function get_post_type_object( $post_type ) {
  643. global $wp_post_types;
  644. if ( empty($wp_post_types[$post_type]) )
  645. return null;
  646. return $wp_post_types[$post_type];
  647. }
  648. /**
  649. * Get a list of all registered post type objects.
  650. *
  651. * @package WordPress
  652. * @subpackage Post
  653. * @since 2.9.0
  654. * @uses $wp_post_types
  655. * @see register_post_type
  656. *
  657. * @param array|string $args An array of key => value arguments to match against the post type objects.
  658. * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
  659. * @param string $operator The logical operation to perform. 'or' means only one element
  660. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  661. * @return array A list of post type names or objects
  662. */
  663. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  664. global $wp_post_types;
  665. $field = ('names' == $output) ? 'name' : false;
  666. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  667. }
  668. /**
  669. * Register a post type. Do not use before init.
  670. *
  671. * A simple function for creating or modifying a post type based on the
  672. * parameters given. The function will accept an array (second optional
  673. * parameter), along with a string for the post type name.
  674. *
  675. *
  676. * Optional $args contents:
  677. *
  678. * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
  679. * - description - A short descriptive summary of what the post type is. Defaults to blank.
  680. * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
  681. * - exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
  682. * - publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as.
  683. * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
  684. * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
  685. * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
  686. * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
  687. * - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities.
  688. * - hierarchical - Whether the post type is hierarchical. Defaults to false.
  689. * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
  690. * - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
  691. * - taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
  692. * - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
  693. * - permalink_epmask - The default rewrite endpoint bitmasks.
  694. * - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $taxonomy as slug.
  695. * - query_var - false to prevent queries, or string to value of the query var to use for this post type
  696. * - can_export - true allows this post type to be exported.
  697. * - show_in_nav_menus - true makes this post type available for selection in navigation menus.
  698. * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
  699. * - _edit_link - URL segement to use for edit link of this post type. Set to 'post.php?post=%d'. THIS IS FOR INTERNAL USE ONLY!
  700. *
  701. * @since 2.9.0
  702. * @uses $wp_post_types Inserts new post type object into the list
  703. *
  704. * @param string $post_type Name of the post type.
  705. * @param array|string $args See above description.
  706. * @return object the registered post type object
  707. */
  708. function register_post_type($post_type, $args = array()) {
  709. global $wp_post_types, $wp_rewrite, $wp;
  710. if ( !is_array($wp_post_types) )
  711. $wp_post_types = array();
  712. // Args prefixed with an underscore are reserved for internal use.
  713. $defaults = array(
  714. 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
  715. '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
  716. 'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
  717. 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
  718. 'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null
  719. );
  720. $args = wp_parse_args($args, $defaults);
  721. $args = (object) $args;
  722. $post_type = sanitize_user($post_type, true);
  723. $args->name = $post_type;
  724. // If not set, default to the setting for public.
  725. if ( null === $args->publicly_queryable )
  726. $args->publicly_queryable = $args->public;
  727. // If not set, default to the setting for public.
  728. if ( null === $args->show_ui )
  729. $args->show_ui = $args->public;
  730. // Whether to show this type in nav-menus.php. Defaults to the setting for public.
  731. if ( null === $args->show_in_nav_menus )
  732. $args->show_in_nav_menus = $args->public;
  733. // If not set, default to true if not public, false if public.
  734. if ( null === $args->exclude_from_search )
  735. $args->exclude_from_search = !$args->public;
  736. if ( empty($args->capability_type) )
  737. $args->capability_type = 'post';
  738. $args->cap = get_post_type_capabilities( $args );
  739. unset($args->capabilities);
  740. if ( ! empty($args->supports) ) {
  741. add_post_type_support($post_type, $args->supports);
  742. unset($args->supports);
  743. } else {
  744. // Add default features
  745. add_post_type_support($post_type, array('title', 'editor'));
  746. }
  747. if ( false !== $args->query_var && !empty($wp) ) {
  748. if ( true === $args->query_var )
  749. $args->query_var = $post_type;
  750. $args->query_var = sanitize_title_with_dashes($args->query_var);
  751. $wp->add_query_var($args->query_var);
  752. }
  753. if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
  754. if ( !is_array($args->rewrite) )
  755. $args->rewrite = array();
  756. if ( !isset($args->rewrite['slug']) )
  757. $args->rewrite['slug'] = $post_type;
  758. if ( !isset($args->rewrite['with_front']) )
  759. $args->rewrite['with_front'] = true;
  760. if ( $args->hierarchical )
  761. $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  762. else
  763. $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  764. $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
  765. }
  766. if ( $args->register_meta_box_cb )
  767. add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
  768. $args->labels = get_post_type_labels( $args );
  769. $args->label = $args->labels->name;
  770. $wp_post_types[$post_type] = $args;
  771. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  772. foreach ( $args->taxonomies as $taxonomy ) {
  773. register_taxonomy_for_object_type( $taxonomy, $post_type );
  774. }
  775. return $args;
  776. }
  777. /**
  778. * Builds an object with all post type capabilities out of a post type object
  779. *
  780. * Accepted keys of the capabilities array in the post type object:
  781. * - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type" (edit_post).
  782. * - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
  783. * - edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
  784. * - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
  785. * - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type" (read_post).
  786. * - read_private_posts - The capability that controls reading private posts. Defaults to "read_private . $capability_type . s" (read_private_posts).
  787. * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post).
  788. *
  789. * @since 3.0.0
  790. * @param object $args
  791. * @return object object with all the capabilities as member variables
  792. */
  793. function get_post_type_capabilities( $args ) {
  794. $defaults = array(
  795. 'edit_post' => 'edit_' . $args->capability_type,
  796. 'edit_posts' => 'edit_' . $args->capability_type . 's',
  797. 'edit_others_posts' => 'edit_others_' . $args->capability_type . 's',
  798. 'publish_posts' => 'publish_' . $args->capability_type . 's',
  799. 'read_post' => 'read_' . $args->capability_type,
  800. 'read_private_posts' => 'read_private_' . $args->capability_type . 's',
  801. 'delete_post' => 'delete_' . $args->capability_type,
  802. );
  803. $labels = array_merge( $defaults, $args->capabilities );
  804. return (object) $labels;
  805. }
  806. /**
  807. * Builds an object with all post type labels out of a post type object
  808. *
  809. * Accepted keys of the label array in the post type object:
  810. * - name - general name for the post type, usually plural. The same and overriden by $post_type_object->label. Default is Posts/Pages
  811. * - singular_name - name for one object of this post type. Default is Post/Page
  812. * - add_new - Default is Add New for both hierarchical and non-hierarchical types. When internationalizing this string, please use a {@link http://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} matching your post type. Example: <code>_x('Add New', 'product');</code>
  813. * - add_new_item - Default is Add New Post/Add New Page
  814. * - edit_item - Default is Edit Post/Edit Page
  815. * - new_item - Default is New Post/New Page
  816. * - view_item - Default is View Post/View Page
  817. * - search_items - Default is Search Posts/Search Pages
  818. * - not_found - Default is No posts found/No pages found
  819. * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
  820. * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
  821. *
  822. * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages.)
  823. *
  824. * @since 3.0.0
  825. * @param object $post_type_object
  826. * @return object object with all the labels as member variables
  827. */
  828. function get_post_type_labels( $post_type_object ) {
  829. $nohier_vs_hier_defaults = array(
  830. 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
  831. 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
  832. 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
  833. 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
  834. 'edit_item' => array( __('Edit Post'), __('Edit Page') ),
  835. 'new_item' => array( __('New Post'), __('New Page') ),
  836. 'view_item' => array( __('View Post'), __('View Page') ),
  837. 'search_items' => array( __('Search Posts'), __('Search Pages') ),
  838. 'not_found' => array( __('No posts found'), __('No pages found') ),
  839. 'not_found_in_trash' => array( __('No posts found in Trash'), __('No pages found in Trash') ),
  840. 'parent_item_colon' => array( null, __('Parent Page:') )
  841. );
  842. return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  843. }
  844. /**
  845. * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  846. *
  847. * @access private
  848. */
  849. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
  850. if ( isset( $object->label ) ) {
  851. $object->labels['name'] = $object->label;
  852. }
  853. if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) {
  854. $object->labels['singular_name'] = $object->labels['name'];
  855. }
  856. $defaults = array_map( create_function( '$x', $object->hierarchical? 'return $x[1];' : 'return $x[0];' ), $nohier_vs_hier_defaults );
  857. $labels = array_merge( $defaults, $object->labels );
  858. return (object)$labels;
  859. }
  860. /**
  861. * Register support of certain features for a post type.
  862. *
  863. * All features are directly associated with a functional area of the edit screen, such as the
  864. * editor or a meta box: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author',
  865. * 'excerpt', 'page-attributes', 'thumbnail', and 'custom-fields'.
  866. *
  867. * Additionally, the 'revisions' feature dictates whether the post type will store revisions,
  868. * and the 'comments' feature dicates whether the comments count will show on the edit screen.
  869. *
  870. * @since 3.0.0
  871. * @param string $post_type The post type for which to add the feature
  872. * @param string|array $feature the feature being added, can be an array of feature strings or a single string
  873. */
  874. function add_post_type_support( $post_type, $feature ) {
  875. global $_wp_post_type_features;
  876. $features = (array) $feature;
  877. foreach ($features as $feature) {
  878. if ( func_num_args() == 2 )
  879. $_wp_post_type_features[$post_type][$feature] = true;
  880. else
  881. $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
  882. }
  883. }
  884. /**
  885. * Remove support for a feature from a post type.
  886. *
  887. * @since 3.0.0
  888. * @param string $post_type The post type for which to remove the feature
  889. * @param string $feature The feature being removed
  890. */
  891. function remove_post_type_support( $post_type, $feature ) {
  892. global $_wp_post_type_features;
  893. if ( !isset($_wp_post_type_features[$post_type]) )
  894. return;
  895. if ( isset($_wp_post_type_features[$post_type][$feature]) )
  896. unset($_wp_post_type_features[$post_type][$feature]);
  897. }
  898. /**
  899. * Checks a post type's support for a given feature
  900. *
  901. * @since 3.0.0
  902. * @param string $post_type The post type being checked
  903. * @param string $feature the feature being checked
  904. * @return boolean
  905. */
  906. function post_type_supports( $post_type, $feature ) {
  907. global $_wp_post_type_features;
  908. if ( !isset( $_wp_post_type_features[$post_type][$feature] ) )
  909. return false;
  910. // If no args passed then no extra checks need be performed
  911. if ( func_num_args() <= 2 )
  912. return true;
  913. // @todo Allow pluggable arg checking
  914. //$args = array_slice( func_get_args(), 2 );
  915. return true;
  916. }
  917. /**
  918. * Updates the post type for the post ID.
  919. *
  920. * The page or post cache will be cleaned for the post ID.
  921. *
  922. * @since 2.5.0
  923. *
  924. * @uses $wpdb
  925. *
  926. * @param int $post_id Post ID to change post type. Not actually optional.
  927. * @param string $post_type Optional, default is post. Supported values are 'post' or 'page' to
  928. * name a few.
  929. * @return int Amount of rows changed. Should be 1 for success and 0 for failure.
  930. */
  931. function set_post_type( $post_id = 0, $post_type = 'post' ) {
  932. global $wpdb;
  933. $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
  934. $return = $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
  935. if ( 'page' == $post_type )
  936. clean_page_cache($post_id);
  937. else
  938. clean_post_cache($post_id);
  939. return $return;
  940. }
  941. /**
  942. * Retrieve list of latest posts or posts matching criteria.
  943. *
  944. * The defaults are as follows:
  945. * 'numberposts' - Default is 5. Total number of posts to retrieve.
  946. * 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  947. * 'category' - What category to pull the posts from.
  948. * 'orderby' - Default is 'post_date'. How to order the posts.
  949. * 'order' - Default is 'DESC'. The order to retrieve the posts.
  950. * 'include' - See {@link WP_Query::query()} for more.
  951. * 'exclude' - See {@link WP_Query::query()} for more.
  952. * 'meta_key' - See {@link WP_Query::query()} for more.
  953. * 'meta_value' - See {@link WP_Query::query()} for more.
  954. * 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
  955. * 'post_parent' - The parent of the post or post type.
  956. * 'post_status' - Default is 'published'. Post status to retrieve.
  957. *
  958. * @since 1.2.0
  959. * @uses $wpdb
  960. * @uses WP_Query::query() See for more default arguments and information.
  961. * @link http://codex.wordpress.org/Template_Tags/get_posts
  962. *
  963. * @param array $args Optional. Overrides defaults.
  964. * @return array List of posts.
  965. */
  966. function get_posts($args = null) {
  967. $defaults = array(
  968. 'numberposts' => 5, 'offset' => 0,
  969. 'category' => 0, 'orderby' => 'post_date',
  970. 'order' => 'DESC', 'include' => array(),
  971. 'exclude' => array(), 'meta_key' => '',
  972. 'meta_value' =>'', 'post_type' => 'post',
  973. 'suppress_filters' => true
  974. );
  975. $r = wp_parse_args( $args, $defaults );
  976. if ( empty( $r['post_status'] ) )
  977. $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
  978. if ( ! empty($r['numberposts']) )
  979. $r['posts_per_page'] = $r['numberposts'];
  980. if ( ! empty($r['category']) )
  981. $r['cat'] = $r['category'];
  982. if ( ! empty($r['include']) ) {
  983. $incposts = wp_parse_id_list( $r['include'] );
  984. $r['posts_per_page'] = count($incposts); // only the number of posts included
  985. $r['post__in'] = $incposts;
  986. } elseif ( ! empty($r['exclude']) )
  987. $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
  988. $r['caller_get_posts'] = true;
  989. $get_posts = new WP_Query;
  990. return $get_posts->query($r);
  991. }
  992. //
  993. // Post meta functions
  994. //
  995. /**
  996. * Add meta data field to a post.
  997. *
  998. * Post meta data is called "Custom Fields" on the Administration Panels.
  999. *
  1000. * @since 1.5.0
  1001. * @uses $wpdb
  1002. * @link http://codex.wordpress.org/Function_Reference/add_post_meta
  1003. *
  1004. * @param int $post_id Post ID.
  1005. * @param string $key Metadata name.
  1006. * @param mixed $value Metadata value.
  1007. * @param bool $unique Optional, default is false. Whether the same key should not be added.
  1008. * @return bool False for failure. True for success.
  1009. */
  1010. function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
  1011. // make sure meta is added to the post, not a revision
  1012. if ( $the_post = wp_is_post_revision($post_id) )
  1013. $post_id = $the_post;
  1014. return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
  1015. }
  1016. /**
  1017. * Remove metadata matching criteria from a post.
  1018. *
  1019. * You can match based on the key, or key and value. Removing based on key and
  1020. * value, will keep from removing duplicate metadata with the same key. It also
  1021. * allows removing all metadata matching key, if needed.
  1022. *
  1023. * @since 1.5.0
  1024. * @uses $wpdb
  1025. * @link http://codex.wordpress.org/Function_Reference/delete_post_meta
  1026. *
  1027. * @param int $post_id post ID
  1028. * @param string $meta_key Metadata name.
  1029. * @param mixed $meta_value Optional. Metadata value.
  1030. * @return bool False for failure. True for success.
  1031. */
  1032. function delete_post_meta($post_id, $meta_key, $meta_value = '') {
  1033. // make sure meta is added to the post, not a revision
  1034. if ( $the_post = wp_is_post_revision($post_id) )
  1035. $post_id = $the_post;
  1036. return delete_metadata('post', $post_id, $meta_key, $meta_value);
  1037. }
  1038. /**
  1039. * Retrieve post meta field for a post.
  1040. *
  1041. * @since 1.5.0
  1042. * @uses $wpdb
  1043. * @link http://codex.wordpress.org/Function_Reference/get_post_meta
  1044. *
  1045. * @param int $post_id Post ID.
  1046. * @param string $key The meta key to retrieve.
  1047. * @param bool $single Whether to return a single value.
  1048. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
  1049. * is true.
  1050. */
  1051. function get_post_meta($post_id, $key, $single = false) {
  1052. return get_metadata('post', $post_id, $key, $single);
  1053. }
  1054. /**
  1055. * Update post meta field based on post ID.
  1056. *
  1057. * Use the $prev_value parameter to differentiate between meta fields with the
  1058. * same key and post ID.
  1059. *
  1060. * If the meta field for the post does not exist, it will be added.
  1061. *
  1062. * @since 1.5
  1063. * @uses $wpdb
  1064. * @link http://codex.wordpress.org/Function_Reference/update_post_meta
  1065. *
  1066. * @param int $post_id Post ID.
  1067. * @param string $key Metadata key.
  1068. * @param mixed $value Metadata value.
  1069. * @param mixed $prev_value Optional. Previous value to check before removing.
  1070. * @return bool False on failure, true if success.
  1071. */
  1072. function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
  1073. // make sure meta is added to the post, not a revision
  1074. if ( $the_post = wp_is_post_revision($post_id) )
  1075. $post_id = $the_post;
  1076. return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
  1077. }
  1078. /**
  1079. * Delete everything from post meta matching meta key.
  1080. *
  1081. * @since 2.3.0
  1082. * @uses $wpdb
  1083. *
  1084. * @param string $post_meta_key Key to search for when deleting.
  1085. * @return bool Whether the post meta key was deleted from the database
  1086. */
  1087. function delete_post_meta_by_key($post_meta_key) {
  1088. if ( !$post_meta_key )
  1089. return false;
  1090. global $wpdb;
  1091. $post_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key));
  1092. if ( $post_ids ) {
  1093. $postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) );
  1094. $in = implode( ',', array_fill(1, count($postmetaids), '%d'));
  1095. do_action( 'delete_postmeta', $postmetaids );
  1096. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN($in)", $postmetaids ));
  1097. do_action( 'deleted_postmeta', $postmetaids );
  1098. foreach ( $post_ids as $post_id )
  1099. wp_cache_delete($post_id, 'post_meta');
  1100. return true;
  1101. }
  1102. return false;
  1103. }
  1104. /**
  1105. * Retrieve post meta fields, based on post ID.
  1106. *
  1107. * The post meta fields are retrieved from the cache, so the function is
  1108. * optimized to be called more than once. It also applies to the functions, that
  1109. * use this function.
  1110. *
  1111. * @since 1.2.0
  1112. * @link http://codex.wordpress.org/Function_Reference/get_post_custom
  1113. *
  1114. * @uses $id Current Loop Post ID
  1115. *
  1116. * @param int $post_id post ID
  1117. * @return array
  1118. */
  1119. function get_post_custom($post_id = 0) {
  1120. global $id;
  1121. if ( !$post_id )
  1122. $post_id = (int) $id;
  1123. $post_id = (int) $post_id;
  1124. if ( ! wp_cache_get($post_id, 'post_meta') )
  1125. update_postmeta_cache($post_id);
  1126. return wp_cache_get($post_id, 'post_meta');
  1127. }
  1128. /**
  1129. * Retrieve meta field names for a post.
  1130. *
  1131. * If there are no meta fields, then nothing (null) will be returned.
  1132. *
  1133. * @since 1.2.0
  1134. * @link http://codex.wordpress.org/Function_Reference/get_post_custom_keys
  1135. *
  1136. * @param int $post_id post ID
  1137. * @return array|null Either array of the keys, or null if keys could not be retrieved.
  1138. */
  1139. function get_post_custom_keys( $post_id = 0 ) {
  1140. $custom = get_post_custom( $post_id );
  1141. if ( !is_array($custom) )
  1142. return;
  1143. if ( $keys = array_keys($custom) )
  1144. return $keys;
  1145. }
  1146. /**
  1147. * Retrieve values for a custom post field.
  1148. *
  1149. * The parameters must not be considered optional. All of the post meta fields
  1150. * will be retrieved and only the meta field key values returned.
  1151. *
  1152. * @since 1.2.0
  1153. * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values
  1154. *
  1155. * @param string $key Meta field key.
  1156. * @param int $post_id Post ID
  1157. * @return array Meta field values.
  1158. */
  1159. function get_post_custom_values( $key = '', $post_id = 0 ) {
  1160. if ( !$key )
  1161. return null;
  1162. $custom = get_post_custom($post_id);
  1163. return isset($custom[$key]) ? $custom[$key] : null;
  1164. }
  1165. /**
  1166. * Check if post is sticky.
  1167. *
  1168. * Sticky posts should remain at the top of The Loop. If the post ID is not
  1169. * given, then The Loop ID for the current post will be used.
  1170. *
  1171. * @since 2.7.0
  1172. *
  1173. * @param int $post_id Optional. Post ID.
  1174. * @return bool Whether post is sticky.
  1175. */
  1176. function is_sticky($post_id = null) {
  1177. global $id;
  1178. $post_id = absint($post_id);
  1179. if ( !$post_id )
  1180. $post_id = absint($id);
  1181. $stickies = get_option('sticky_posts');
  1182. if ( !is_array($stickies) )
  1183. return false;
  1184. if ( in_array($post_id, $stickies) )
  1185. return true;
  1186. return false;
  1187. }
  1188. /**
  1189. * Sanitize every post field.
  1190. *
  1191. * If the context is 'raw', then the post object or array will get minimal santization of the int fields.
  1192. *
  1193. * @since 2.3.0
  1194. * @uses sanitize_post_field() Used to sanitize the fields.
  1195. *
  1196. * @param object|array $post The Post Object or Array
  1197. * @param string $context Optional, default is 'display'. How to sanitize post fields.
  1198. * @return object|array The now sanitized Post Object or Array (will be the same type as $post)
  1199. */
  1200. function sanitize_post($post, $context = 'display') {
  1201. if ( is_object($post) ) {
  1202. // Check if post already filtered for this context
  1203. if ( isset($post->filter) && $context == $post->filter )
  1204. return $post;
  1205. if ( !isset($post->ID) )
  1206. $post->ID = 0;
  1207. foreach ( array_keys(get_object_vars($post)) as $field )
  1208. $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
  1209. $post->filter = $context;
  1210. } else {
  1211. // Check if post already filtered for this context
  1212. if ( isset($post['filter']) && $context == $post['filter'] )
  1213. return $post;
  1214. if ( !isset($post['ID']) )
  1215. $post['ID'] = 0;
  1216. foreach ( array_keys($post) as $field )
  1217. $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
  1218. $post['filter'] = $context;
  1219. }
  1220. return $post;
  1221. }
  1222. /**
  1223. * Sanitize post field based on context.
  1224. *
  1225. * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The
  1226. * 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display'
  1227. * when calling filters.
  1228. *
  1229. * @since 2.3.0
  1230. * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and
  1231. * $post_id if $context == 'edit' and field name prefix == 'post_'.
  1232. *
  1233. * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'.
  1234. * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'post_'.
  1235. * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
  1236. *
  1237. * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything
  1238. * other than 'raw', 'edit' and 'db' and field name prefix == 'post_'.
  1239. * @uses apply_filters() Calls 'post_$field' passing $value if $context == anything other than 'raw',
  1240. * 'edit' and 'db' and field name prefix != 'post_'.
  1241. *
  1242. * @param string $field The Post Object field name.
  1243. * @param mixed $value The Post Object value.
  1244. * @param int $post_id Post ID.
  1245. * @param string $context How to sanitize post fields. Looks for 'raw', 'edit', 'db', 'display',
  1246. * 'attribute' and 'js'.
  1247. * @return mixed Sanitized value.
  1248. */
  1249. function sanitize_post_field($field, $value, $post_id, $context) {
  1250. $int_fields = array('ID', 'post_parent', 'menu_order');
  1251. if (