wp_edit_posts_query

Run the wp query to fetch the posts for listing on the edit posts page.

Description

(array) wp_edit_posts_query( (bool) $q = false ); 

Returns (array)

Parameters (1)

0. $q — Optional. (bool) => false
Array of query variables to use to build the query or false to use $_GET superglobal.

Usage

  1. if ( !function_exists( 'wp_edit_posts_query' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // Array of query variables to use to build the query or false to use $_GET superglobal. 
  5. $q = false; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_edit_posts_query($q); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/post.php  
  1. function wp_edit_posts_query( $q = false ) { 
  2. if ( false === $q ) 
  3. $q = $_GET; 
  4. $q['m'] = isset($q['m']) ? (int) $q['m'] : 0; 
  5. $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0; 
  6. $post_stati = get_post_stati(); 
  7.  
  8. if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) ) 
  9. $post_type = $q['post_type']; 
  10. else 
  11. $post_type = 'post'; 
  12.  
  13. $avail_post_stati = get_available_post_statuses($post_type); 
  14.  
  15. if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) { 
  16. $post_status = $q['post_status']; 
  17. $perm = 'readable'; 
  18.  
  19. if ( isset( $q['orderby'] ) ) { 
  20. $orderby = $q['orderby']; 
  21. } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ) ) ) { 
  22. $orderby = 'modified'; 
  23.  
  24. if ( isset( $q['order'] ) ) { 
  25. $order = $q['order']; 
  26. } elseif ( isset( $q['post_status'] ) && 'pending' == $q['post_status'] ) { 
  27. $order = 'ASC'; 
  28.  
  29. $per_page = "edit_{$post_type}_per_page"; 
  30. $posts_per_page = (int) get_user_option( $per_page ); 
  31. if ( empty( $posts_per_page ) || $posts_per_page < 1 ) 
  32. $posts_per_page = 20; 
  33.  
  34. /** 
  35. * Filters the number of items per page to show for a specific 'per_page' type. 
  36. * The dynamic portion of the hook name, `$post_type`, refers to the post type. 
  37. * Some examples of filter hooks generated here include: 'edit_attachment_per_page',  
  38. * 'edit_post_per_page', 'edit_page_per_page', etc. 
  39. * @since 3.0.0 
  40. * @param int $posts_per_page Number of posts to display per page for the given post 
  41. * type. Default 20. 
  42. */ 
  43. $posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page ); 
  44.  
  45. /** 
  46. * Filters the number of posts displayed per page when specifically listing "posts". 
  47. * @since 2.8.0 
  48. * @param int $posts_per_page Number of posts to be displayed. Default 20. 
  49. * @param string $post_type The post type. 
  50. */ 
  51. $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type ); 
  52.  
  53. $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page'); 
  54.  
  55. // Hierarchical types require special args. 
  56. if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) { 
  57. $query['orderby'] = 'menu_order title'; 
  58. $query['order'] = 'asc'; 
  59. $query['posts_per_page'] = -1; 
  60. $query['posts_per_archive_page'] = -1; 
  61. $query['fields'] = 'id=>parent'; 
  62.  
  63. if ( ! empty( $q['show_sticky'] ) ) 
  64. $query['post__in'] = (array) get_option( 'sticky_posts' ); 
  65.  
  66. wp $query ); 
  67.  
  68. return $avail_post_stati;