meta_value是序列化对象时,如何过滤帖子?

时间:2019-05-03 10:20:55

标签: wordpress

我正在对自定义帖子类型进行下拉,以仅过滤内容是否完成。

我的meta_value是一个像add_action('restrict_manage_posts',[ $this, 'dropdown_status_filter']); add_filter( 'parse_query', [ $this, 'filter_request_query']) public function dropdown_status_filter($post_type){ if('ctp_dasboard' !== $post_type) { return; //filter your post } $selected = ''; $request_attr = 'status_filter'; if ( isset($_REQUEST[$request_attr]) ) { $selected = $_REQUEST[$request_attr]; } echo '<select id="status_filter-loc" name="status_filter">'; echo ($selected === '1') ? "'<option value='1' selected>Done</option>'" : "'<option value='1'>Done</option>'"; echo ($selected === '2') ? "'<option value='2' selected>Pending</option>'" : "'<option value='2'>Pending</option>'"; echo '</select>'; } public function filter_request_query($query){ //modify the query only if it admin and main query. if( !(is_admin() AND $query->is_main_query()) ){ return $query; } //we want to modify the query for the targeted custom post and filter option if( !('ctp_dasboard' === $query->query['post_type'] AND isset($_REQUEST['status_filter']) ) ){ return $query; } //for the default value of our filter no modification is required if(0 == $_REQUEST['status_filter']){ return $query; } //modify the query_vars. // $value = ""; // var_dump($_REQUEST['status_filter'] === '1'); if ( $_REQUEST['status_filter'] === '1'){ // global $wpdb; // $meta_key = 'form_ctp'; // $meta_value = '"token";N;'; // $mid = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value LIKE %s ", $meta_key, $meta_value) ); // dd($mid); // // The Query $args = [ 'post_type' => 'ctp_dasboard', ['meta_query' => [ 'key' => 'form_ctp', 'value' => 'token";N;', // this is when token is null 'compare' => 'IN' ]] ]; // dd($args); $query = new \WP_Query( $args ); // dd($the_query); } if ( $_REQUEST['status_filter'] === '2'){ } return $query; } 这样的序列化对象 我的工作是检查令牌是否为空

enter image description here

CustomPostTypeController.php

FEED_EXPORT_ENCODING = "utf-8"

1 个答案:

答案 0 :(得分:0)

有一个特殊功能maybe_unserialize,它可以返回未序列化的数据数组/字符串等。Check documentation。您的变量应如下所示:

$str = "a:3{s:5:"email";s:21:"mrsxcvsfesr@gmail.com";s:9:"store";s:6:"testes";s:5:"token";s:34:"$P$B7efpLZUWWyB4QkhG0YaYyIwXRAj.3.";}";
$data = maybe_unserialize($str); // will return an array