Query string form $_GET['value'] is not working as meta value in wp_query

Here is my code.How strange! if I put any static value replacing $s_string, it's working fine. Thanks in advance fo

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
  • You haven’t defined $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:16
  • Yes. I have defined it like $s_string = $_GET['q']; – Rafiq Commented May 22, 2019 at 12:33
  • First things first, you really should be developing with a good debugging tool like XDebug that will allow you to step through your code line by line. Alternatively, try adding error_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
Add a comment  | 

1 Answer 1

Reset to default 0

Are 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信