函数文档

get_post_states()

💡 云策文档标注

概述

get_post_states() 函数用于从指定文章对象中检索文章状态数组,返回以状态为键的标签数组。该函数主要用于文章列表表格中显示状态标签,并可通过 display_post_states 过滤器进行自定义。

关键要点

  • 函数接受一个 WP_Post 对象作为必需参数,返回字符串数组,键为状态标识,值为翻译后的状态标签。
  • 支持多种状态检测,包括受密码保护、私有、草稿、待审、置顶、定时发布等,以及特殊页面如首页、文章页、隐私政策页。
  • 包含 display_post_states 过滤器,允许开发者修改默认状态映射,过滤器参数为状态数组和文章对象。
  • 函数内部使用 get_post_meta、get_option、is_sticky 等辅助函数来获取文章元数据、选项和置顶状态。

代码示例

function get_post_states( $post ) {
    $post_states = array();

    if ( isset( $_REQUEST['post_status'] ) ) {
        $post_status = $_REQUEST['post_status'];
    } else {
        $post_status = '';
    }

    if ( ! empty( $post->post_password ) ) {
        $post_states['protected'] = _x( 'Password protected', 'post status' );
    }

    if ( 'private' === $post->post_status && 'private' !== $post_status ) {
        $post_states['private'] = _x( 'Private', 'post status' );
    }

    if ( 'draft' === $post->post_status ) {
        if ( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
            $post_states[] = __( 'Customization Draft' );
        } elseif ( 'draft' !== $post_status ) {
            $post_states['draft'] = _x( 'Draft', 'post status' );
        }
    } elseif ( 'trash' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
        $post_states[] = _x( 'Customization Draft', 'post status' );
    }

    if ( 'pending' === $post->post_status && 'pending' !== $post_status ) {
        $post_states['pending'] = _x( 'Pending', 'post status' );
    }

    if ( is_sticky( $post->ID ) ) {
        $post_states['sticky'] = _x( 'Sticky', 'post status' );
    }

    if ( 'future' === $post->post_status ) {
        $post_states['scheduled'] = _x( 'Scheduled', 'post status' );
    }

    if ( 'page' === get_option( 'show_on_front' ) ) {
        if ( (int) get_option( 'page_on_front' ) === $post->ID ) {
            $post_states['page_on_front'] = _x( 'Front Page', 'page label' );
        }

        if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
            $post_states['page_for_posts'] = _x( 'Posts Page', 'page label' );
        }
    }

    if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) {
        $post_states['page_for_privacy_policy'] = _x( 'Privacy Policy Page', 'page label' );
    }

    return apply_filters( 'display_post_states', $post_states, $post );
}

注意事项

  • 函数自 WordPress 5.3.0 版本引入,使用时需确保环境版本兼容。
  • display_post_states 过滤器在 2.8.0 版本添加,3.6.0 版本增加了 $post 参数,5.5.0 版本扩展至 Customizer 上下文,使用时应检查函数存在性。
  • 状态检测逻辑依赖于文章对象的属性(如 post_status、post_password)和全局请求参数,需注意上下文影响。

📄 原文内容

Retrieves an array of post states from a post.

Parameters

$postWP_Postrequired
The post to retrieve states for.

Return

string[] Array of post state labels keyed by their state.

Source

function get_post_states( $post ) {
	$post_states = array();

	if ( isset( $_REQUEST['post_status'] ) ) {
		$post_status = $_REQUEST['post_status'];
	} else {
		$post_status = '';
	}

	if ( ! empty( $post->post_password ) ) {
		$post_states['protected'] = _x( 'Password protected', 'post status' );
	}

	if ( 'private' === $post->post_status && 'private' !== $post_status ) {
		$post_states['private'] = _x( 'Private', 'post status' );
	}

	if ( 'draft' === $post->post_status ) {
		if ( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
			$post_states[] = __( 'Customization Draft' );
		} elseif ( 'draft' !== $post_status ) {
			$post_states['draft'] = _x( 'Draft', 'post status' );
		}
	} elseif ( 'trash' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
		$post_states[] = _x( 'Customization Draft', 'post status' );
	}

	if ( 'pending' === $post->post_status && 'pending' !== $post_status ) {
		$post_states['pending'] = _x( 'Pending', 'post status' );
	}

	if ( is_sticky( $post->ID ) ) {
		$post_states['sticky'] = _x( 'Sticky', 'post status' );
	}

	if ( 'future' === $post->post_status ) {
		$post_states['scheduled'] = _x( 'Scheduled', 'post status' );
	}

	if ( 'page' === get_option( 'show_on_front' ) ) {
		if ( (int) get_option( 'page_on_front' ) === $post->ID ) {
			$post_states['page_on_front'] = _x( 'Front Page', 'page label' );
		}

		if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
			$post_states['page_for_posts'] = _x( 'Posts Page', 'page label' );
		}
	}

	if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) {
		$post_states['page_for_privacy_policy'] = _x( 'Privacy Policy Page', 'page label' );
	}

	/**
	 * Filters the default post display states used in the posts list table.
	 *
	 * @since 2.8.0
	 * @since 3.6.0 Added the `$post` parameter.
	 * @since 5.5.0 Also applied in the Customizer context. If any admin functions
	 *              are used within the filter, their existence should be checked
	 *              with `function_exists()` before being used.
	 *
	 * @param array<string, string>  $post_states A mapping of post state slugs to translated post state labels.
	 *                                            E.g. `array( 'draft' => __( 'Draft' ), 'sticky' => __( 'Sticky' ), ... )`.
	 * @param WP_Post                $post        The current post object.
	 */
	return apply_filters( 'display_post_states', $post_states, $post );
}

Hooks

apply_filters( ‘display_post_states’, array<string, , WP_Post $post )

Filters the default post display states used in the posts list table.

Changelog

Version Description
5.3.0 Introduced.