WP_POSTMETA-这些值在数据结构中意味着什么?

时间:2021-06-25 作者:user1669296

在wp\\U Posteta表中,我在一个名为;meta\\u值;。数据的结构令人困惑。我试图理解这些价值观的含义:

meta\\u键字段的值为_“雇主社会”;

a:4:{i:0;a:2:{s:7:"network";s:8:"facebook";s:3:"url";s:1:"#";}i:1;a:2:{s:7:"network";s:7:"twitter";s:3:"url";s:1:"#";}i:2;a:2:{s:7:"network";s:8:"linkedin";s:3:"url";s:1:"#";}i:3;a:2:{s:7:"network";s:9:"instagram";s:3:"url";s:1:"#";}}
具体来说,a:4:是什么意思,s:8,还是i:1;a: 2:

我看到了脸书和推特,可以推断它们与一家公司的社交媒体处理有关,但其结构/格式令人困惑。一定有办法破译这个?

我更大的目标是从外部源向该字段插入数据,但如果我无法理解它,我该如何做?

1 个回复
SO网友:Dave Romsey

WordPress将在保存到post meta时序列化数组。存储在键下的数据_employer_socials 在您的示例中看起来like this:

Array
(
    [0] => Array
        (
                [network] => facebook
                [url] => #
        )

    [1] => Array
        (
                [network] => twitter
                [url] => #
        )

    [2] => Array
        (
                [network] => linkedin
                [url] => #
        )

    [3] => Array
        (
                [network] => instagram
                [url] => #
        )
)
当您获得meta时,WP将为您取消序列化,如下所示:

$employer_socials = get_post_meta( get_the_ID(), \'_employer_socials\', true );
然后,您可以将数据作为常规数组处理。保存阵列时,WP将为您序列化它。以下是一些可以作为起点使用的基本函数:

/**
 * Updates an existing network, or adds a new one if it does not exist.
 *
 * @param int    $post_id The Post ID to be updated.
 * @param string $network The name of the network to check.
 *
 * @return bool True if an update was made, false otherwise.
 */
function wpse_update_employee_social( $post_id, $network, $url ) {

    $employer_socials = get_post_meta( $post_id, \'_employer_socials\', true );

    // Update.
    if ( wpse_has_employer_social( $post_id, $network ) ) {
        foreach ( $employer_socials as $key => $employer_social ) {
            if ( $employer_social[\'network\'] === $network ) {
                $employer_socials[ $key ][\'url\'] = $url;
                update_post_meta( $post_id, \'_employer_socials\', $employer_socials );
                return true;
            }
        }
    } else {
        // Add.
        $employer_socials[] = [
            \'network\' => $network,
            \'url\'     => $url,
        ];
        update_post_meta( $post_id, \'_employer_socials\', $employer_socials );
        return true;
    }

    // Nothing was updated.
    return false;
}

/**
 * Checks if a given network exists.
 *
 * @param int    $post_id The Post ID to be updated.
 * @param string $network The name of the network to check.
 *
 * @return bool True if the network exits, false otherwise.
 */
function wpse_has_employer_social( $post_id, $network ) {
    $employer_socials = get_post_meta( $post_id, \'_employer_socials\', true );
    foreach ( $employer_socials as $employer_social ) {
        if ( $employer_social[\'network\'] === $network ) {
            return true;
        }
    }

    return false;
}
添加新员工社交的示例:

wpse_update_employee_social( 5302, \'wpse\', \'https://wordpress.stackexchange.com/users/208214/user1669296\' );
更新员工社交网站的示例:

wpse_update_employee_social( 5302, \'facebook\', \'#1234\' );

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在