WordPress custom search results

I have a WordPress custom search form which is used to search for floors into various buildings. For example, when a user searches for a floor between 1000 and 1500 sq ft, he will get a list of all the buildings with free floor spaces between those values.

The search values get stored inside cookies as I will need them on the single building page.

Read More

On the single building page there is a section showing the floors that match the user’s search criteria.

It works fine with the floors that I have added some time ago. When I add new floors they don’t show up on the single building page

What could that be? Here is the code I’ve written to show the matching floors to the single building page:

<table class="table" id="tab1C">
    <?php include( get_template_directory().'/inc/function-search.php' );
    $categories = get_the_category();
    $category_id = $categories[0]->cat_name;
    $args = array(
        'meta_query'    =>  array(
                                $sq_array,
                                $mt_array,
                                $prelet_array,
                                $date_array,
                            ),
        'category_name' => $category_id,
    );

    $i = 0;
    $search = array_merge($args); 
    $propertySearch = new WP_Query( $search ); ?>
    <?php $terms = get_the_terms( get_the_ID(), 'category');
    if( !empty($terms) ) : $term = array_pop($terms); ?>
        <?php if( $propertySearch->have_posts() ) : while( $propertySearch->have_posts() ) : $propertySearch->the_post(); ?>
            <?php get_template_part( 'content', 'table' ); ?>
            <?php $i++; ?>
        <?php endwhile; endif;  
        wp_reset_postdata(); ?>
    <?php endif; ?>
</table>

This is the function-search.php include

<?php

$_minsq     = $_GET['minsq'] != '' ? $_GET['minsq'] : '';
$_maxsq     = $_GET['maxsq'] != '' ? $_GET['maxsq'] : '';
$_minmt     = $_GET['minmt'] != '' ? $_GET['minmt'] : '';
$_maxmt     = $_GET['maxmt'] != '' ? $_GET['maxmt'] : '';
$unit   = $_GET['unit'] != '' ? $_GET['unit'] : '';
$prelet     = $_GET['prelet'] != '' ? $_GET['prelet'] : '';
$date   = $_GET['date'] != '' ? $_GET['date'] : '';

$sorter     = $_GET['sorter'] != '' ? $_GET['sorter'] : '';

$sq_array       = array();
$mt_array       = array();
$prelet_array   = array();
$date_array     = array();
$sorter_array   = array();


if ( isset( $_COOKIE["unit"] )){
    if ($_COOKIE["unit"] == "ft") {
        if ( isset( $_COOKIE["minsq"] )){
            $value_min = intval( $_COOKIE["minsq"] );
            //update_post_meta( $post->ID, 'wpcf-square-feet', $value_min );  
        }
        if ( isset( $_COOKIE["maxsq"] )){
            $value_max = intval( $_COOKIE["maxsq"] );
            //update_post_meta( $post->ID, 'wpcf-square-feet', $value_max );  
        }

        $sq_array = array(
            array (
                'key'     => 'wpcf-square-feet',
                'value'   => array( $value_min, $value_max ),
                'type'    => 'numeric',
                'compare' => 'BETWEEN',
            ),
            array(
                'key'     => 'wpcf-square-feet',
                'value'   => $value_min,
                'type'    => 'numeric',
                'compare' => '>=',
            ),
            array(
                'key'     => 'wpcf-square-feet',
                'value'   => $value_max,
                'type'    => 'numeric',
                'compare' => '<=',
            )
        );
    } elseif ($_COOKIE["unit"] == "mt") {
        if ( isset( $_COOKIE["minmt"] )){
            $value_minmt = intval( $_COOKIE["minmt"] );
            //update_post_meta( $post->ID, 'wpcf-square-meters', $value_minmt );  
        }
        if ( isset( $_COOKIE["maxmt"] )){
            $value_maxmt = intval( $_COOKIE["maxmt"] );
            //update_post_meta( $post->ID, 'wpcf-square-meters', $value_maxmt );  
        }

        $mt_array = array(
            array (
                'key'     => 'wpcf-square-meters',
                'value'   => array( $value_minmt, $value_maxmt ),
                'type'    => 'numeric',
                'compare' => 'BETWEEN',
            ),
            array(
                'key'     => 'wpcf-square-meters',
                'value'   => $value_minmt,
                'type'    => 'numeric',
                'compare' => '>=',
            ),
            array(
                'key'     => 'wpcf-square-meters',
                'value'   => $value_maxmt,
                'type'    => 'numeric',
                'compare' => '<=',
            )
        );
    }
}

if ( isset( $_COOKIE["prelet"] )){
    if ($_COOKIE["prelet"] != "yes") {
        $value_prelet = 2;
        //update_post_meta( $post->ID, 'wpcf-prelet', $value_prelet );

        $prelet_array = array(
            'key'     => 'wpcf-prelet',
            'value'   => $value_prelet,
            'type'    => 'numeric',
            'compare' => '=',
        );
    }   
};


if ( isset( $_COOKIE["date"] ) &&   $date != ""){
    $value_date = intval( $_COOKIE["date"] );
    //update_post_meta( $post->ID, 'wpcf-completition-date', $value_date ); 

    $date_array = array(
        'key'     => 'wpcf-completition-date',
        'value'   => $value_date,
        'type'    => 'numeric',
        'compare' => '=',
    );
};

if ( isset( $sorter )){
    if ($sorter == "0") {
        $sorter_array = array(
            'meta_key'  => 'wpcf-square-feet',
            'orderby'   => 'meta_value_num',
            'order'         => 'DESC'
        );
    } elseif ($sorter == "1") {
        $sorter_array = array(
            'meta_key'  => 'wpcf-square-feet',
            'orderby'   => 'meta_value_num',
            'order'         => 'ASC'
        );
    }
}

?>

This is the single building page where the floor shows up

enter image description here

And this is the single building page where the floor don’t show up

enter image description here

They both use the same code and have the same search criteria but it doesn’t work with newly added floors.

I hope this all makes sense and there will be a good soul that will be able to help me out. I have to deliver this by tomorrow morning at 9am uk time and I’ve been trying to fix it all day with no success. I would really appreciate any help, thank you.

Related posts

1 comment

  1. The issue was with the category name, it started with a number..! I changed it into a string and it now works. 5 hours later….

Comments are closed.