#wordpress
#wordpress
Вопрос:
Я хочу искать сообщения по типу пользовательского поля. У меня есть пользовательское поле «country» с пользовательским типом сообщения umrahpackage, теперь я хочу, чтобы при поиске посетителя по стране «abc» в нем отображались все сообщения со страной abc.
вот мой код, но он не работает. `
add_shortcode('user_search','My_User_search');
function My_User_search($atts = null)
{
$out = user_search_form();
$args = array(
'post_type' => 'umrahpackage',
'meta_query' => array(
array(
'key' => 'country',
'value' => 'pakistan',
'compare' => 'LIKE',
))
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
$out .= '<li>' . get_the_title() . '</li>';
endwhile;
endif;
return $out;
}
//function to display user search form
function user_search_form(){
$metavalue = $metakey = '';
if (isset($_GET['search_by'])){
$metakey = $_GET['search_by'];
}
if (isset($_GET['s_value'])){
$metavalue = $_GET['s_value'];
}
$re = '<div class="user_search"><form action="" name="user_s" method="get">
<label for="search_by">Search by:</label>
<select id="search_by" name="search_by">';
if ($metakey != ''){
$re.= '"';
$re.= ($metakey == "country") ;
}else{
$re .= '
<option value="country">Comapny Name</option>';
}
$re .= '
</select>
<label>Company Name</label>
<input id="s_value" name="s_value" type="text" value="'.$metavalue.'"/>
<input type="hidden" id="user_search" name="post_type" value="umrahpackage" />
<input id="submit" type="submit" value="Search" />
</form></div>';
return $re;
}`
Комментарии:
1. если вы хотите получить значение из формы, вам нужно отредактировать аргументы запроса ‘value’ => $_POST[‘s_value’], проверьте значение post с помощью isset перед qyery
Ответ №1:
Я решил это, я поделюсь этим, если это поможет кому-то еще
add_shortcode('user_search','My_User_search');
function My_User_search($atts = null){
$out = user_search_form();
$args = array('post_type' => 'umrahpackage','order' => 'asc',
'meta_query' => array(
array(
'key' => 'country',
'value' => $_GET['s_value'],
'compare' => 'Like',
)
)
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if($_GET['s_value']==''){
//before search hide the posts
}
else {
$out .= '<li>' . get_the_title() . '</li>';
}
endwhile;
endif;
return $out;
}
function user_search_form(){
$re = '<div class="user_search"><form action="" name="user_s" method="get">
<label for="search_by">Search by:</label>
<div id="search_by" name="search_by">';
$re .= '
<label>Company Name</label>
<input id="s_value" name="s_value" type="text" value="'.$metavalue.'"/>
<input name="user_search" id="user_search" type="hidden" value="umrahpackage"/>
<input id="submit" type="submit" value="Search" />
</form></div>';
return $re;
}
?>