从数组 Wordpress 中的对象中获取值


Get value from Objects in an Array Wordpress

我正在尝试向Wordpress网站添加投票功能,可用的插件不符合我的要求。

我有这个数组,其中包含另一个带有wp_post对象的数组(这些是投票的用户),我需要做的是将current_user_ID与数组对象中的所有post_author ID 进行比较。我需要这样做,以便我可以向已经投票的用户显示不同的内容。只要知道如何从数组中的对象中获取post_author值并将其与current_user_id进行比较就会有很大帮助。希望你们能帮到我,提前谢谢。

这就是我获取数组的方式:

<?php $my_post_meta = get_post_meta($post->ID, 'supporters', false);
            echo print_r($my_post_meta); ?>

这是显示以下内容的数组:

 Array ([0] => Array
        (
        [0] => WP_Post Object
            (
                [ID] => 750
                [post_author] => 46
            )
        [1] => WP_Post Object
            (
                [ID] => 749
                [post_author] => 47
            )
        [2] => WP_Post Object
            (
                [ID] => 748
                [post_author] => 1
            )
          ))1            

试试这个:

 <?php 
    $my_post_meta = get_post_meta($post->ID, 'supporters', false);
    $current_user_ID = get_current_user_id();
    foreach( $my_post_meta[0] as $post_object ) {
        if( $post_object->ID == $current_user_ID ) {
            // The current user has voted
        }
    }
   ?>