Here is my code.How strange! if I put any static value replacing $s_string, it's working fine. Thanks in advance for any help.
$s_string = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
$custom_fields = new WP_Query(array(
'post_type' => 'post_type',
'posts_per_page' => -1,
'post_status' => 'publish',
));
$fields = $custom_fields->posts;
$post_ids = array();
foreach ($fields as $post) {
$post_ids[] = $post->ID;
}
if( count( $post_ids ) > 1 ) {
$sub_meta_queries = array();
foreach( $post_ids as $value ) {
$sub_meta_queries[] = array(
'key' => $value,
'value' => $s_string,
'compare' => 'LIKE'
);
}
$meta_queries[] = array_merge( array( 'relation' => 'OR' ), $sub_meta_queries );
} else {
$meta_queries[] = array(
'key' => $post_ids,
'value' => sanitize_text_field( $s_string ),
'compare' => 'LIKE'
);
}
Here is my code.How strange! if I put any static value replacing $s_string, it's working fine. Thanks in advance for any help.
$s_string = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
$custom_fields = new WP_Query(array(
'post_type' => 'post_type',
'posts_per_page' => -1,
'post_status' => 'publish',
));
$fields = $custom_fields->posts;
$post_ids = array();
foreach ($fields as $post) {
$post_ids[] = $post->ID;
}
if( count( $post_ids ) > 1 ) {
$sub_meta_queries = array();
foreach( $post_ids as $value ) {
$sub_meta_queries[] = array(
'key' => $value,
'value' => $s_string,
'compare' => 'LIKE'
);
}
$meta_queries[] = array_merge( array( 'relation' => 'OR' ), $sub_meta_queries );
} else {
$meta_queries[] = array(
'key' => $post_ids,
'value' => sanitize_text_field( $s_string ),
'compare' => 'LIKE'
);
}
Share
Improve this question
edited May 22, 2019 at 13:51
Rafiq
asked May 22, 2019 at 12:11
RafiqRafiq
1001 gold badge1 silver badge9 bronze badges
3
|
1 Answer
Reset to default 0Are you sure the $_GET['q']
param is set?
Try:
$s_string = ( isset($_GET['q]) && !empty($_GET['q']) ) ? sanitize_text_field($_GET['q']) : '';
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745473358a4629233.html
$s_string
anywhere, so I’m not sure what you’re expecting to happen? What is that variable supposed to be? – Jacob Peattie Commented May 22, 2019 at 12:16error_log( $_GET['q'] );
and check your PHP error log to see what the value of$_GET['q']
is. – jasonp Commented May 23, 2019 at 22:17