2015-06-30 191 views
2

如何從posts_where裏面獲取帖子ID?從posts_where裏面獲取帖子id wordpress

在我的代碼下面,$ page_id = $ wp_query-> get_queried_object_id();在頁面上工作,但不是帖子。我已經設置了第二個變量$ post_id,並希望用該帖子的ID填充該變量。

這裏是我的功能/過濾器:

// Filter posts 
    function ugrestriction_filter($where, $wp_query) 
    { 
     global $wpdb, $post, $current_user, $wp_query; 
     get_currentuserinfo(); 
     $user_id = $current_user->ID; 
     $page_id = $wp_query->get_queried_object_id(); 
     $post_id = $wp_query->get_queried_object_id(); 

     $meta_key1 = '_ugrestriction'; 
     $meta_key1_value = 'on'; 

     $postids=$wpdb->get_col($wpdb->prepare( 
      "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = 'on'", "_ugrestriction" 
     )); 


     //$postids = array("70", "1", "2", "3"); 

     if (in_array($post_id, $postids)) { 
      $filter = 'on'; 
     } elseif (in_array($page_id, $postids)) { 
      $filter = 'on'; 
     } 


     if ($filter == 'on') { 
      $where .= " AND ($wpdb->postmeta.meta_key = '_ugrestriction' AND $wpdb->postmeta.meta_value = 'on' AND " . $wpdb->posts . " .post_author =". $user_id .")"; 
     } 

      return $where; 
    } 

    add_filter('posts_where', 'ugrestriction_filter', 10, 2); 
+0

你要明白:第一,一個帖子的ID是因爲版本追蹤的wordpress做有用的少。更好的問題可能是如何得到一個職位的slu?? – ArtisticPhoenix

+0

一個slu would在這種情況下不會幫助我......我需要一個ID ...... – user123123

回答

0
// Filter posts 
    function ugrestriction_filter($where, $wp_query) 
    { 
     global $wpdb, $post, $current_user, $wp_query; 
     get_currentuserinfo(); 
     $user_id = $current_user->ID; 
     $page_id = $wp_query->get_queried_object_id(); 
     $post_id = $post->ID; 

     $meta_key1 = '_ugrestriction'; 
     $meta_key1_value = 'on'; 

     $postids=$wpdb->get_col($wpdb->prepare( 
      "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = 'on'", "_ugrestriction" 
     )); 


     //$postids = array("70", "1", "2", "3"); 

     if (in_array($post_id, $postids)) { 
      $filter = 'on'; 
     } elseif (in_array($page_id, $postids)) { 
      $filter = 'on'; 
     } 


     if ($filter == 'on') { 
      $where .= " AND ($wpdb->postmeta.meta_key = '_ugrestriction' AND $wpdb->postmeta.meta_value = 'on' AND " . $wpdb->posts . " .post_author =". $user_id .")"; 
     } 

      return $where; 
    } 

    add_filter('posts_where', 'ugrestriction_filter', 10, 2); 
+0

This Works!看起來像我的問題是$ postids。當在一個頁面上,這可以工作,但不是在帖子上。 – user123123