• I have created a custom search on a page template and have date, destination (custom taxonmy) and also Origin (custom taxonomy) this are custom post type.

    Here is a screenshot https://i.stack.imgur.com/afbZo.jpg

    When I search by date it duplicates the post 4times, when you search it by destination it works but when you search it by destination and orgin also it will not. As well as when when you search it by date, destination and origin in a time it does not display a post.

    Here is my code:

    function get_reviews_by_custom_search() {
    global $wpdb;
    $dates = $_GET['dates'];
    $destination = $_GET['destination'];
    $origincity = $_GET['origincity'];  
    
        if ($dates != '') {
    
                   $enddate = date("Y-m-t", strtotime($dates));
                   $dates_sql = " AND $wpdb->posts.post_date >= '$dates' AND $wpdb->posts.post_date < '$enddate'";
        }
        if ($destination != '' && $destination != -1) {
    
                    $destination_sql = "AND $wpdb->terms.term_id = '$destination'";
        }
        if ($origincity != '' && $origincity != -1) {
                    $origincity_sql = "AND $wpdb->terms.term_id = '$origincity'";
        }   
    
        $querystr = "
            SELECT *
    FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE $wpdb->posts.post_type = 'truckers'
    AND $wpdb->posts.post_status = 'publish'
    " . $dates_sql . "
    " . $destination_sql . "
    " . $origincity_sql . "
    ORDER BY $wpdb->posts.post_date DESC
        ";
    
        $searched_posts = $wpdb->get_results($querystr);
        return $searched_posts;
    }

    Any help is highly appreciated. Thanks.

  • The topic ‘Search on page template by date and custom taxonomy’ is closed to new replies.