How can I filter results depending on post data using selects and inputs?

I have a form that contains 3 selects and 3 inputs, plus the submit button. When I submit the form I’m trying to filter the search results by all these inputs and select $_POST values, but when I don’t complete all the inputs and select an option for every select I have no results. What am I doing wrong here:

<div style='float: left; margin-right: 20px;'>
	<form action='' method="post">
		<select name='post_type'>
			<option selected="true" disabled="disabled">Alege post type</option>    
			<option <?php if(isset($_POST['post_type']) && $_POST['post_type'] == 'post') echo 'selected'; ?> value='post'>Post</option>
			<option <?php if(isset($_POST['post_type']) && $_POST['post_type'] == 'page') echo 'selected'; ?> value='page'>Page</option>
			<option <?php if(isset($_POST['post_type']) && $_POST['post_type'] == 'retete') echo 'selected'; ?> value='retete'>Retete</option>
		</select>
		<select name='post_status'>
			<option selected="true" disabled="disabled">Alege status</option>    
			<option <?php if(isset($_POST['post_status']) && $_POST['post_status'] == 'publish') echo 'selected'; ?> value='publish'>Publish</option>
			<option <?php if(isset($_POST['post_status']) && $_POST['post_status'] == 'trash') echo 'selected'; ?> value='trash'>Trash</option>
			<option <?php if(isset($_POST['post_status']) && $_POST['post_status'] == 'draft') echo 'selected'; ?> value='draft'>Draft</option>
		</select>
		<select name='meta_key'>
			<option selected="true" disabled="disabled">Alege meta_key</option>    
			<option <?php if(isset($_POST['meta_key']) && $_POST['meta_key'] == 'reteta_vizual') echo 'selected'; ?> value='reteta_vizual'>reteta_vizual</option>
			<option <?php if(isset($_POST['meta_key']) && $_POST['meta_key'] == 'ingrediente') echo 'selected'; ?> value='ingrediente'>ingrediente</option>
			<option <?php if(isset($_POST['meta_key']) && $_POST['meta_key'] == 'preparare') echo 'selected'; ?> value='preparare'>preparare</option>
		</select>
		<input type='text' name='date_begin' placeholder='Data inceput ("yyyy/mm/dd")' style='width: 200px;'>
		<input type='text' name='date_end' placeholder='Data sfarsit ("yyyy/mm/dd")' style='width: 200px;'>
		<input type='text' name='meta_value' placeholder='Meta value here'>
		<input type='submit' name='filter_results' value='Filtreaza rezultate'>
	</form>
</div>
	<?php
	//filter 
	echo "<table class='wp-list-table widefat fixed posts'>";
		echo "<thead>";
			echo "<tr>";
				echo "<th>ID</th>";
				echo "<th>Post title</th>";
				echo "<th>Author</th>";
			echo "</tr>";
		echo "</thead>";
	if(isset($_POST['filter_results'])){
		$tabel_1 = $wpdb->prefix . 'posts';
		$tabel_2 = $wpdb->prefix . 'postmeta';
		$post_status = $_POST['post_status'];
		$post_type = $_POST['post_type'];
		$meta_key = $_POST['meta_key'];
		$date_begin = $_POST['date_begin'];
		$date_end = $_POST['date_end'];
		var_dump($_POST);
		$records_posts = $wpdb->get_results("SELECT * FROM $tabel_1 AS posts 
											INNER JOIN $tabel_2 AS postmeta 
											ON posts.ID = postmeta.post_id 
											WHERE posts.post_status = '$post_status' 
											AND posts.post_type = '$post_type' 
											AND postmeta.meta_key = '$meta_key'
											AND posts.post_date BETWEEN '$date_begin' AND '$date_end'
											GROUP BY posts.ID");
		echo "<pre>";
		var_dump($records_posts);
		foreach($records_posts as $single_post){
			$author_id = $single_post->post_author;
			$author_name = get_the_author_meta('nicename', $author_id);
			echo "<tr>";
				echo "<td>" . $single_post->post_id . "</td>";
				echo "<td>" . $single_post->post_title . "</td>";
				echo "<td>" . $author_name . "</td>";
			echo "</tr>";
		}
	}
	echo "</table>";

Related posts

1 comment

  1. You should check the blank values in query like below :

    $all_conditions = '';
    
    if($post_status != "")
        $conditions[] = "posts.post_status = '".$post_status."'";
    
    if($post_type != "")
        $conditions[] = "posts.post_type = '".$post_type."'";
    
    if($meta_key != "")
        $conditions[] = "postmeta.meta_key = '".$meta_key."'";
    
    if($date_begin != "" && $date_end != "")
        $conditions[] = "( posts.post_date BETWEEN '".$date_begin."' AND '".$date_end."' )";
    
    if(count($conditions)>0)
        $all_conditions = implode(" and ",$conditions);
    
    if($all_conditions != "")
        $all_conditions = "where ".$all_conditions;
    
    $sql = "SELECT * FROM $tabel_1 AS posts INNER JOIN $tabel_2 AS postmeta ON posts.ID = postmeta.post_id $all_conditions GROUP BY posts.ID"
    
    $records_posts = $wpdb->get_results($sql);
    

Comments are closed.