• Resolved OniX777

    (@onix777)


    Hello. I use a plugin with a custom search query (vin) that I specified in the settings. In the “Average no. of results” field, for some reason it always 1, although in fact the search result is zero. How can I fix this? Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Cornel Raiu

    (@cornelraiu-1)

    Hello @onix777 ,

    Thank you for reaching out.

    This usually happens when the plugin you are using for search uses a completely custom function for searching and doesn’t use the WordPress flow for this.

    Can you give me the name of the plugin to see if I can replicate the issue and, why not, come up with a solution for you?

    Thank you!

    Thread Starter OniX777

    (@onix777)

    I use custom code to search by custom fields. Apparently you need to somehow pass the postfound value to your variable.

    <?php 
    if (0==0){
    ?>
    <p><strong>CВЕДЕНИЯ О ТРАНСПОРТНОМ СРЕДСТВЕ:</strong></p>

    <form action="/searchdtp/" method="get" id="searchdtp">
    <p><input required type="text" name="vin" value="" placeholder="VIN или Номер кузова"></p>
    <p>
    <input id="zapvin" type="submit" class="button" value="Запросить" onclick="ym(24241753,'reachGoal','zapvin'); return true;" />
    </p>
    </form>

    <?php
    $postfound=0;

    for ($x=1; $x<5; $x++) {

    $args = array('post_type' => 'post',
    'category_name' => 'avarii',
    'meta_query' => array(
    array(
    'key' => 'avto'.$x.'_vin',
    'value' => $_GET['vin'],
    ),
    ),
    );
    query_posts( $args ); ?>

    <?php admired_content_nav( 'nav-above' ); ?>
    ??????
    <?php

    $err=$_SERVER['REQUEST_URI'];
    $err = substr($err, -1);
    $wp_query->max_num_pages=1;

    while (have_posts() and $err!='/') : the_post(); $postfound=1;?>

    <?php get_template_part( 'loop', get_post_format() ); ?>

    <?php endwhile; // end of the loop. ?>

    <?php /* Display navigation when applicable */ ?>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
    <?php /* add page navigation to indexes */ ?>
    <?php if (function_exists("admired_pagination")) {
    admired_pagination($wp_query->max_num_pages);
    }
    endif; ?>
    <?php
    }
    ?> <!-- endfor-->

    $err=$_SERVER['REQUEST_URI'];
    $err = substr($err, -1);

    if ( $postfound != 1 and $err!='/') : ?>?

    <article id="post-0" class="post no-results not-found">
    <div class="entry-content">
    <p><strong><?php _e( 'Ничего не найдено. Попробуйте снова.', 'admired' ); ?></strong></p>
    </div><!-- .entry-content -->
    </article><!-- #post-0 -->
    <?php endif;

    }
    Plugin Author Cornel Raiu

    (@cornelraiu-1)

    Got it! Thank you for the snippet.

    What I can see here is that you are running the search by overriding the main $wp_query which, in this case, I would not do that.

    I have made some changes to your code snippet, so it uses a completely custom query for searching through the VIN entries without altering the main $wp_query.

    Also, I added the wp_reset_postdata() function after your loop, so the global $post is not altered after exiting the loop: https://developer.www.ads-software.com/reference/functions/wp_reset_postdata/

    Another thing, I changed the $postfound variable to $posts_found where we add all the found posts coming from the for loop. That way we can store the search with the number of total found posts (.just like a regular search would ) using the following function:

    if ( class_exists( 'MWTSA_Process_Query' ) ) {
    (new MWTSA_Process_Query())->process_search_term( $vin, $found_posts );
    }

    Here is the complete code from after the form display

    <?php
    $posts_found = 0;
    $vin = sanitize_text_field($_GET['vin']);

    for ( $x = 1; $x < 5; $x ++ ) {
    $args = array(
    'post_type' => 'post',
    'category_name' => 'avarii',
    'meta_query' => array(
    array(
    'key' => 'avto' . $x . '_vin',
    'value' => $vin,
    ),
    ),
    );

    $custom_query = new WP_Query();
    $custom_query->query( $args );

    admired_content_nav( 'nav-above' );

    $err = $_SERVER['REQUEST_URI'];
    $err = substr( $err, - 1 );

    $custom_query->max_num_pages = 1;
    $posts_found += $custom_query->found_posts;

    while ( $custom_query->have_posts() and $err != '/' ) : $custom_query->the_post(); ?>

    <?php get_template_part( 'loop', get_post_format() ); ?>

    <?php endwhile; // end of the loop. ?>

    <?php /* Display navigation when applicable */ ?>
    <?php if ( $custom_query->max_num_pages > 1 ) : ?>
    <?php /* add page navigation to indexes */ ?>
    <?php if ( function_exists( "admired_pagination" ) ) {
    admired_pagination( $custom_query->max_num_pages );
    }
    endif; ?>
    <?php
    } /* endfor */

    wp_reset_postdata();

    if ( class_exists( 'MWTSA_Process_Query' ) ) {
    (new MWTSA_Process_Query())->process_search_term( $vin, $posts_found );
    }

    $err=$_SERVER['REQUEST_URI'];
    $err = substr($err, -1);

    if ( $posts_found === 0 and $err!='/') : ?>

    <article id="post-0" class="post no-results not-found">
    <div class="entry-content">
    <p><strong><?php _e( 'Ничего не найдено. Попробуйте снова.', 'admired' ); ?></strong></p>
    </div><!-- .entry-content -->
    </article><!-- #post-0 -->
    <?php endif;

    I tested this with a custom query on my end and it worked as expected. Again, very important to remove the vin from the custom search query setting.

    Please let me know how that went.

    • This reply was modified 2 months, 1 week ago by Cornel Raiu.
    Thread Starter OniX777

    (@onix777)

    Works great now! Thank you very much!

    Plugin Author Cornel Raiu

    (@cornelraiu-1)

    Great! Glad I could help!

    If you found my plugin useful, please post a review.
    That’s the way you can help the plugin grow.

    Thank you!

    Cornel Raiu

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.