2017-01-07 56 views
0

我使用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); 
} 

功能返回一個數字,但總是比註冊元數據/點擊的實際數量低得多。任何人看到問題?

編輯:我敢肯定,問題是我得到的帖子總數,而不是總數的元數據條目/點擊 - 這往往不是幾個每個職位。有什麼辦法呢?

回答

0

我從來沒有找到一種解決方案來查詢元數據並獲得每個帖子的x個結果,所以不是在單個鍵中使用數組,而是將帖子元數據分成三個鍵。然後我使用$ wpdb進行自定義查詢。 tell_nedlastinger()函數的最終代碼:

$brukerid = (string)get_current_user_id(); 

global $wpdb; 
$query = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')"); 

return count($query);