如何从posts_where中获取帖子ID?
在我的代码中,$ 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 :(得分: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 );