无法将Wordpress帖子ID发送到Echo或插入数据库

时间:2014-02-22 20:16:11

标签: php mysql wordpress

我已经编写了这样的代码,当点击链接时,应该将当前用户的id,当前帖子的id和日期插入数据库。当前用户ID&当前日期被插入数据库并完美地回显,但是我无法将当前帖子的id设置为“echo”或将其插入数据库中。这是我到目前为止的代码:

global $post;
    global $wpdb;
    $wp_user_id = get_current_user_id(); 
    $activity_post_id = $wp_query->post->ID;
    $activity_added = current_time('mysql', 1);

    echo "<h2>The Current User ID is: ". $wp_user_id . "</h2>";
    echo "<h2>The Current Activity ID is: ". $activity_post_id . "</h2>";
    echo "<h2>The Current Date / Time is: ". $activity_added . "</h2>";

    $wpdb->insert(
        'wp_growwellactivities',
        array(
            'wp_user_id' => $wp_user_id,
            'activity_post_id' => $activity_post_id,
            'date_added' => $activity_added
        )
    ); 

1 个答案:

答案 0 :(得分:2)

您尝试在不使用全局的情况下访问wp_query。

将全局变更为:

global $post, $wp_query, $wpdb;

或者改为使用它:

$activity_post_id = $post->ID;