pods_by_title

A fork of get_page_by_title that excludes items unavailable via access rights (by status).

Description

(WP_Post|null) pods_by_title( (string) $title, (constant) $output = OBJECT, (string) $type = 'page', (null) $status = null ); 

Returns (WP_Post|null)

WP_Post on success or null on failure

Parameters (4)

0. $title (string)
Title of item to get
1. $output — Optional. (constant) => OBJECT
Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
2. $type — Optional. (string) => 'page'
The type.
3. $status — Optional. (null) => null
Post statuses to include (default is what user has access to)

Usage

  1. if ( !function_exists( 'pods_by_title' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'pods-custom-content-types-and-fields/includes/general.php'; 
  3.  
  4. // Title of item to get 
  5. $title = ''; 
  6.  
  7. // Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT. 
  8. $output = OBJECT; 
  9.  
  10. // The type. 
  11. $type = 'page'; 
  12.  
  13. // Post statuses to include (default is what user has access to) 
  14. $status = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = pods_by_title($title, $output, $type, $status); 
  18.  

Defined (1)

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

/includes/general.php  
  1. function pods_by_title ( $title, $output = OBJECT, $type = 'page', $status = null ) { 
  2. // @todo support Pod item lookups, not just Post Types 
  3.  
  4. /** 
  5. * @var $wpdb WPDB 
  6. */ 
  7. global $wpdb; 
  8.  
  9. if ( empty( $status ) ) { 
  10. $status = array( 
  11. 'publish' 
  12. ); 
  13.  
  14. if ( current_user_can( 'read_private_' . $type . 's') ) 
  15. $status[] = 'private'; 
  16.  
  17. if ( current_user_can( 'edit_' . $type . 's' ) ) 
  18. $status[] = 'draft'; 
  19.  
  20. $status = (array) $status; 
  21.  
  22. $status_sql = ' AND `post_status` IN ( %s' . str_repeat( ', %s', count( $status ) -1 ) . ' )'; 
  23.  
  24. $orderby_sql = ' ORDER BY ( `post_status` = %s ) DESC' . str_repeat( ', ( `post_status` = %s ) DESC', count( $status ) - 1 ) . ', `ID` DESC'; 
  25.  
  26. $prepared = array_merge( array( $title, $type ), $status, $status ); // once for WHERE, once for ORDER BY 
  27.  
  28. $page = $wpdb->get_var( $wpdb->prepare( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_title` = %s AND `post_type` = %s" . $status_sql . $orderby_sql, $prepared ) ); 
  29.  
  30. if ( $page ) 
  31. return get_post( $page, $output ); 
  32.  
  33. return null;