从自定义字段中读取数组

时间:2016-07-05 04:57:27

标签: php arrays wordpress

我按当前用户的名字保存帖子。此外,我将数组对象 - $ganalytics_settings - 保存到自定义字段中的帖子。使用以下代码,我尝试加载用户名的数组,具有特定的帖子标题:

    function get_custom_field_for_current_user( $customFieldName ) {

        $current_user = wp_get_current_user();          
        global $wpdb;
        $id = $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = '" . $current_user->user_login . "' && post_status = 'draft' && post_type = 'post' ", 'ARRAY_N');

        return get_post_meta((int)$id[0], $customFieldName);
    }

但是,我要回到以下对象:

enter image description here

相反,我想取回以下内容:

enter image description here

为什么我的函数给了我一个数组中的数组的任何建议?

感谢您的回复!

2 个答案:

答案 0 :(得分:1)

我不知道我是否理解你,但是如果你的函数返回第一个键是数组的数组,那么只需要总是返回第一个键

/** adds a tooltip to a specified element, like an event or resource */
function addToolTip(element, o, target, hideEvents) {
    if (target === undefined || target === null) {
        target = false;
    }
    if (hideEvents === undefined) {
        hideEvents = "mousedown mouseup mouseleave";
    }
    element.qtip({
        content: {
            title: o.title,
            text: o.text
        }
        , position: {
            my: "bottom center",
            at: "top center",
            target: target // "mouse" is buggy when dragging and interferes with clicking
            //adjust: { y: -9}
        }
        , style: {
            tip: { corner: 'bottom center' },
            classes: 'qtip-light qtip-shadow'
        },
        show: {
            effect: function () {
                $(this).fadeTo(300, 1);
            },
            solo: true,
            delay: 200
        },
        hide: {
            effect: true,
            event: hideEvents // otherwise stays while dragging
        }
    });
}

答案 1 :(得分:1)

函数get_post_meta具有以下签名get_post_meta ( int $post_id, string $key = '', bool $single = false )。如果$single为false,则返回值为数组。如果$single为真,则将是元数据字段的值。

试试这个:

function get_custom_field_for_current_user( $customFieldName ) {

    $current_user = wp_get_current_user();          
    global $wpdb;
    $id = $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = '" . $current_user->user_login . "' && post_status = 'draft' && post_type = 'post' ", 'ARRAY_N');

    return get_post_meta((int)$id[0], $customFieldName, true);
                                                      //^ Here to return single value
}