【发布时间】:2017-05-22 10:14:34
【问题描述】:
我正在使用 WordPress 元数据来记录对图片的点击,以了解每个用户点击了哪些图片 - 以及每个用户点击的图片总数。第一部分很好,但我正在努力让计数器运转,因为它返回的元数据量比实际存在的要少。
我有一个自定义帖子类型gallerier,每个画廊都有许多图片。我正在使用元键 nedlasting,并通过获取 url 来单独识别每个图像。
这是我注册点击的方法,在检查它还没有之后:
// Add meta query if it doesnt already exist
function sjekk_nedlasting( $postid, $url, $dato) {
$brukerid = (string)get_current_user_id();
// Check if the image is downloaded previously
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $url),
'compare' => 'LIKE'
),
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// Perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
// If not already downloaded, register it
if ( empty( $nedl_ids ) ) {
$metaarray = Array(
'user_id' => $brukerid,
'url' => $url,
'date' => $dato
);
add_post_meta( $postid, 'nedlasting', $metaarray );
}
}
然后我尝试使用以下函数计算注册点击次数:
// Count number of downloads for a single user
function tell_nedlastinger() {
$brukerid = (string)get_current_user_id();
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
return count($nedl_ids);
}
该函数返回一个数字,但总是远低于注册元数据/点击的实际数量。有人看到问题了吗?
编辑:我很确定问题在于我得到的是帖子总数,而不是元数据条目/点击的总数 - 通常不是每个邮政。有什么办法吗?
【问题讨论】:
标签: wordpress