2016年07月27日 22:54
add_filter('query_vars', function($vars){
array_push($vars, 'my_meta_key');
return $vars;
});
add_action('restrict_manage_posts', function(){
printf(
'',
'my_meta_key',
esc_attr(get_query_var('my_meta_key'))
);
});
add_filter('posts_where', function( $where ) {
global $wpdb;
if ( !is_admin() )
return $where;
$value = get_query_var('my_meta_key');
if ( !empty($value) ) {
$where .= $wpdb->prepare("
AND EXISTS (
SELECT 'x'
FROM {$wpdb->postmeta} as m
WHERE m.post_id = {$wpdb->posts}.ID
AND otwp_posts.post_name like %s
)",
"%{$value}%"
);
}
return $where;
});