wp_get_custom_css_post

The timeline below displays how wordpress function wp_get_custom_css_post has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Fetches the `custom_css` post for a given theme.
 *
 * @since 4.7.0
 *
 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the active theme.
 * @return WP_Post|null The custom_css post or null if none exists.
 */
function wp_get_custom_css_post($stylesheet = '')
{
    if (empty($stylesheet)) {
        $stylesheet = get_stylesheet();
    }
    $custom_css_query_vars = array('post_type' => 'custom_css', 'post_status' => get_post_stati(), 'name' => sanitize_title($stylesheet), 'posts_per_page' => 1, 'no_found_rows' => true, 'cache_results' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'lazy_load_term_meta' => false);
    $post = null;
    if (get_stylesheet() === $stylesheet) {
        $post_id = get_theme_mod('custom_css_post_id');
        if ($post_id > 0 && get_post($post_id)) {
            $post = get_post($post_id);
        }
        // `-1` indicates no post exists; no query necessary.
        if (!$post && -1 !== $post_id) {
            $query = new WP_Query($custom_css_query_vars);
            $post = $query->post;
            /*
             * Cache the lookup. See wp_update_custom_css_post().
             * @todo This should get cleared if a custom_css post is added/removed.
             */
            set_theme_mod('custom_css_post_id', $post ? $post->ID : -1);
        }
    } else {
        $query = new WP_Query($custom_css_query_vars);
        $post = $query->post;
    }
    return $post;
}

WordPress Version: 5.5

/**
 * Fetches the `custom_css` post for a given theme.
 *
 * @since 4.7.0
 *
 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
 * @return WP_Post|null The custom_css post or null if none exists.
 */
function wp_get_custom_css_post($stylesheet = '')
{
    if (empty($stylesheet)) {
        $stylesheet = get_stylesheet();
    }
    $custom_css_query_vars = array('post_type' => 'custom_css', 'post_status' => get_post_stati(), 'name' => sanitize_title($stylesheet), 'posts_per_page' => 1, 'no_found_rows' => true, 'cache_results' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'lazy_load_term_meta' => false);
    $post = null;
    if (get_stylesheet() === $stylesheet) {
        $post_id = get_theme_mod('custom_css_post_id');
        if ($post_id > 0 && get_post($post_id)) {
            $post = get_post($post_id);
        }
        // `-1` indicates no post exists; no query necessary.
        if (!$post && -1 !== $post_id) {
            $query = new WP_Query($custom_css_query_vars);
            $post = $query->post;
            /*
             * Cache the lookup. See wp_update_custom_css_post().
             * @todo This should get cleared if a custom_css post is added/removed.
             */
            set_theme_mod('custom_css_post_id', $post ? $post->ID : -1);
        }
    } else {
        $query = new WP_Query($custom_css_query_vars);
        $post = $query->post;
    }
    return $post;
}

WordPress Version: 4.9

/**
 * Fetch the `custom_css` post for a given theme.
 *
 * @since 4.7.0
 *
 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
 * @return WP_Post|null The custom_css post or null if none exists.
 */
function wp_get_custom_css_post($stylesheet = '')
{
    if (empty($stylesheet)) {
        $stylesheet = get_stylesheet();
    }
    $custom_css_query_vars = array('post_type' => 'custom_css', 'post_status' => get_post_stati(), 'name' => sanitize_title($stylesheet), 'posts_per_page' => 1, 'no_found_rows' => true, 'cache_results' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'lazy_load_term_meta' => false);
    $post = null;
    if (get_stylesheet() === $stylesheet) {
        $post_id = get_theme_mod('custom_css_post_id');
        if ($post_id > 0 && get_post($post_id)) {
            $post = get_post($post_id);
        }
        // `-1` indicates no post exists; no query necessary.
        if (!$post && -1 !== $post_id) {
            $query = new WP_Query($custom_css_query_vars);
            $post = $query->post;
            /*
             * Cache the lookup. See wp_update_custom_css_post().
             * @todo This should get cleared if a custom_css post is added/removed.
             */
            set_theme_mod('custom_css_post_id', $post ? $post->ID : -1);
        }
    } else {
        $query = new WP_Query($custom_css_query_vars);
        $post = $query->post;
    }
    return $post;
}

WordPress Version: 7.1

/**
 * Fetch the `custom_css` post for a given theme.
 *
 * @since 4.7.0
 * @access public
 *
 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
 * @return WP_Post|null The custom_css post or null if none exists.
 */
function wp_get_custom_css_post($stylesheet = '')
{
    if (empty($stylesheet)) {
        $stylesheet = get_stylesheet();
    }
    $custom_css_query_vars = array('post_type' => 'custom_css', 'post_status' => get_post_stati(), 'name' => sanitize_title($stylesheet), 'posts_per_page' => 1, 'no_found_rows' => true, 'cache_results' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'lazy_load_term_meta' => false);
    $post = null;
    if (get_stylesheet() === $stylesheet) {
        $post_id = get_theme_mod('custom_css_post_id');
        if ($post_id > 0 && get_post($post_id)) {
            $post = get_post($post_id);
        }
        // `-1` indicates no post exists; no query necessary.
        if (!$post && -1 !== $post_id) {
            $query = new WP_Query($custom_css_query_vars);
            $post = $query->post;
            /*
             * Cache the lookup. See wp_update_custom_css_post().
             * @todo This should get cleared if a custom_css post is added/removed.
             */
            set_theme_mod('custom_css_post_id', $post ? $post->ID : -1);
        }
    } else {
        $query = new WP_Query($custom_css_query_vars);
        $post = $query->post;
    }
    return $post;
}

WordPress Version: 4.7

/**
 * Fetch the `custom_css` post for a given theme.
 *
 * @since 4.7.0
 * @access public
 *
 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
 * @return WP_Post|null The custom_css post or null if none exists.
 */
function wp_get_custom_css_post($stylesheet = '')
{
    if (empty($stylesheet)) {
        $stylesheet = get_stylesheet();
    }
    $custom_css_query_vars = array('post_type' => 'custom_css', 'post_status' => get_post_stati(), 'name' => sanitize_title($stylesheet), 'posts_per_page' => 1, 'no_found_rows' => true, 'cache_results' => true, 'update_post_meta_cache' => false, 'update_term_meta_cache' => false);
    $post = null;
    if (get_stylesheet() === $stylesheet) {
        $post_id = get_theme_mod('custom_css_post_id');
        if ($post_id > 0 && get_post($post_id)) {
            $post = get_post($post_id);
        } else {
            $query = new WP_Query($custom_css_query_vars);
            $post = $query->post;
            /*
             * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update().
             * @todo This should get cleared if a custom_css post is added/removed.
             */
            if ($post) {
                set_theme_mod('custom_css_post_id', $post->ID);
            } elseif (-1 !== $post_id) {
                set_theme_mod('custom_css_post_id', -1);
            }
        }
    } else {
        $query = new WP_Query($custom_css_query_vars);
        $post = $query->post;
    }
    return $post;
}