如果post_where中的逻辑没有工作wordpress

时间:2015-06-30 14:14:33

标签: php wordpress plugins

在查看帖子时,ugrestriction_filter函数中的我的if($ filter ==' on')似乎无法在此代码中使用。我从functions.php执行此操作它在查看页面时起作用。我怎样才能做到这一点?

这是我的代码:

// Join metadata
function ugrestriction_join($join) {
global $wp_query, $wpdb;

    $join .= "LEFT JOIN ($wpdb->postmeta) ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";


return $join;
}

add_filter('posts_join', 'ugrestriction_join');


// Prevent duplicates

function ugrestriction_distinct( $where ) {
global $wpdb;

    return "DISTINCT";

return $where;
}
add_filter( 'posts_distinct', 'ugrestriction_distinct' );


    // 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 );

1 个答案:

答案 0 :(得分:3)

来自文档:

  

检索帖子的某些功能不会运行过滤器,所以   您附加的posts_where过滤器函数不会修改查询。至   克服了这一点,在参数数组中将suppress_filters设置为false   传递给函数

Source

你试过这个吗?