wp_get_custom_css_post

Fetch the `custom_css` post for a given theme.

Description

(WP_Post|null) wp_get_custom_css_post( (string) $stylesheet = '' ); 

Returns (WP_Post|null)

The custom_css post or null if none exists.

Parameters (1)

0. $stylesheet — Optional. (string) => ''
A theme object stylesheet name. Defaults to the current theme.

Usage

  1. if ( !function_exists( 'wp_get_custom_css_post' ) ) { 
  2. require_once ABSPATH . WPINC . '/theme.php'; 
  3.  
  4. // Optional. A theme object stylesheet name. Defaults to the current theme. 
  5. $stylesheet = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_get_custom_css_post($stylesheet); 
  9.  

Defined (1)

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

/wp-includes/theme.php  
  1. function wp_get_custom_css_post( $stylesheet = '' ) { 
  2. if ( empty( $stylesheet ) ) { 
  3. $stylesheet = get_stylesheet(); 
  4.  
  5. $custom_css_query_vars = array( 
  6. 'post_type' => 'custom_css',  
  7. 'post_status' => get_post_stati(),  
  8. 'name' => sanitize_title( $stylesheet ),  
  9. 'posts_per_page' => 1,  
  10. 'no_found_rows' => true,  
  11. 'cache_results' => true,  
  12. 'update_post_meta_cache' => false,  
  13. 'update_post_term_cache' => false,  
  14. 'lazy_load_term_meta' => false,  
  15. ); 
  16.  
  17. $post = null; 
  18. if ( get_stylesheet() === $stylesheet ) { 
  19. $post_id = get_theme_mod( 'custom_css_post_id' ); 
  20.  
  21. if ( $post_id > 0 && get_post( $post_id ) ) { 
  22. $post = get_post( $post_id ); 
  23.  
  24. // `-1` indicates no post exists; no query necessary. 
  25. if ( ! $post && -1 !== $post_id ) { 
  26. $query = new WP_Query( $custom_css_query_vars ); 
  27. $post = $query->post; 
  28. /** 
  29. * Cache the lookup. See wp_update_custom_css_post(). 
  30. * @todo This should get cleared if a custom_css post is added/removed. 
  31. */ 
  32. set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 ); 
  33. } else { 
  34. $query = new WP_Query( $custom_css_query_vars ); 
  35. $post = $query->post; 
  36.  
  37. return $post;