0

I have a wordpress plugin which will search students result from database.IF i enter Roll number, It will display the result.Now, I would like to modify this as if i enter mobile phone number of student ,it should also show the result. The code is below

    enter // ResultPress search function
function result_press_search() {
    if ( ! isset( $_POST['search'] ) )
        exit;

    query_posts(
            array(
            'posts_per_page' => 1,
            'no_found_rows' => true,
            'post_type' => 'rrf-result',
            'meta_key' => 'roll',
            'meta_value' => wp_unslash( ( string ) $_POST['search'] ),
            'rrf_result_year' => wp_unslash( ( string ) $_POST['result_year'] ),
        )
    );

    include_once( 'templates/marksheet-theme-1.php' );
    exit;
}
2
  • Very hard to help if we don't know what the database looks like... but my guess is that you would want to change the meta_key to match the phone field and the meta_value to get the phone number value. If you want to have multiple meta queries, you will need to use Meta Queries -> codex.wordpress.org/Class_Reference/WP_Meta_Query
    – gdaniel
    Aug 7, 2017 at 16:16
  • Yes, I need multiple.If i change the "roll" to "info_3" , it searches Mobiles number.but i need both. I am not sure about Meta Queries .Appreciate if you give an example.
    – Jaisal
    Aug 8, 2017 at 7:37

1 Answer 1

0

You will need to switch between using query_posts (not recommended), to using WP_Query.

Using WP Query is similar to query_posts, you will pass an array of arguments, and include the meta_query args. Something like:

$args = array(
    'posts_per_page' => 1,
    'no_found_rows' => true,
    'post_type' => 'rrf-result',
    'rrf_result_year' => wp_unslash( ( string ) $_POST['result_year'] ),
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'roll',
            'value' => wp_unslash( ( string ) $_POST['search'] ),
        ),
        array(
            'key' => 'info_3',
            'value' => wp_unslash( ( string ) $_POST['search'] )
        )
    ) 
);



$results = new WP_Query($args);

If you have any results, you can access them by manually looping through $results->posts or by using the wordpress methods:

if($results->have_posts()):
    while($results->have_posts():
        $results->the_post();
        // do something here
    endwhile;
endif;

In your case I don't know exactly what you need because you are including another template part, and I don't know what's in it. I am also not sure if the plugin you are using modifies the query in any way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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