0

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'
            );
        }
  • 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 May 22 at 12:16
  • Yes. I have defined it like $s_string = $_GET['q']; – Rafiq May 22 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 May 23 at 22:17
-1

Are you sure the $_GET['q'] param is set? Try:

$s_string = ( isset($_GET['q]) && !empty($_GET['q']) ) ? sanitize_text_field($_GET['q']) : '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.