I want to search posts by custom field type.
I have custom field "country "with a custom post type umrahpackage now I want if a visitor search for a country "abc" its should show all posts with abc country.
here is my code but not working.
`
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;
}`
I solved it I share it if its help some one else
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;
}
?>
Related
I'm trying to make the default wp_dropdown_categories select dropdown as a radio input with submit button.
This is on the front end.
<form id="filter-select" class="filter-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<?php wp_dropdown_categories(); ?>
<input type="submit" name="submit" value="view" />
</form>
Then in my functions.php I tried to do this
add_filter( 'wp_dropdown_cats', 'dropdown_filter', 10, 2);
function dropdown_filter( $output, $r ) {
$output = preg_replace( '/<option/i', '<input type="radio"', $output );
$output = str_replace( 'class="level-0"', 'name="filter"', $output );
$output = str_replace( "value=\"{$value}\"", "value=\"{$value}\" selected", $output );
return $output;
}
This actually works in swapping out the select dropbox for a radio field. But it does not actually work correctly.
Instead of going to /category/CATEGORYNAME - Like the default select box does.
It goes /?filter=6&submit=view
Any advice is greatly appreciated, thank you.
Try with a custom Walker to output category names instead of IDs.
<div class="radiobuttons">
<?php
$args = array(
'orderby' => 'name',
'hide_title_if_empty' => true,
'title_li' => '',
'walker' => new List_Categories_Radiobuttons
);
wp_list_categories( $args );
?>
</div>
The custom Walker in functions.php:
/**
* Custom Walker to list categories with radio buttons
*/
class List_Categories_Radiobuttons extends Walker_Category {
function start_el(&$output, $category, $depth=0, $args=array()) {
$category_name = esc_attr( $category->name );
$radiobutton = '<input type="radio" name="filtercategory" value="' . $category_name . '">';
$output .= '<div class="radiobutton">' . $radiobutton;
}
function end_el(&$output, $category, $depth=0, $args=array()) {
$output .= "</div>\n";
}
}
Does this point you in the right direction?
I am using this repeater plugin with a custom wordpress template https://github.com/DubFriend
/jquery.repeater
<div class="repeater">
<div data-repeater-list="category-group">
<div data-repeater-item>
<input type="hidden" name="id" id="cat-id"/>
<input type="text" name="cat-title" />
<input type="text" name="cat-slug" />
<input data-repeater-delete type="button" value="Delete"/>
</div>
</div>
<input data-repeater-create type="button" value="Add"/>
</div>
but values not saving in database
In save post function i have
add_shortcode('album','ty_front_end_form');
function ty_save_post_data() {
if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') ) {
print 'Sorry, your nonce did not verify.';
exit;
} else {
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
exit;
}
$release= $_POST['custom_release'];
$artist = $_POST['custom_arname'];
$lang = $_POST['custom_lang'];
$id = $_POST['id'];
$catt = $_POST['cat-title'];
$cats = $_POST['cat-slug'];
$post = array(
'post_title' => wp_strip_all_tags( $title ),
'post_status' => 'publish',
'post_type' => 'album',
'meta_input' => array(
'custom_release' => $release,
'custom_arname' => $artist,
'custom_lang' => $lang,
'id' => $id,
'cat-title' => $catt,
'cat-slug' => $cats
)
);
$post_id = wp_insert_post($post);
$location = home_url();
echo "<meta http-equiv='refresh' content='0;url=$location' />";
exit;
} // end IF
}
and this is a shortcode for frontend user posting which is showing on a template page
I need to create a custom WordPress search form, with multiple text input fields. Each text field should search its corresponding meta key. Frontend would look like this:
Search form:
<form role="search" action="/" method="get" id="searchform">
<input type="text" name="s" placeholder="Name"/>
<input type="text" name="hometown" placeholder="Hometown"/>
<input type="text" name="branch" placeholder="Branch of Service"/>
<input type="text" name="birth" placeholder="Birth Year"/>
<input type="text" name="casualty" placeholder="Casualty Year"/>
<input type="text" name="location" placeholder="Casualty Location">
<input type="hidden" name="post_type" value="veterans" />
<input type="submit" alt="Search" value="Search" />
</form>
The "Name" field should search the post title only. The rest of the input fields would search a specific custom meta key. It would use an "AND" relationship when multiple fields are used.
Is this possible? Here is what I have tried, but it doesn't search if the name ("s") is empty, and it doesn't seem to be affected at all by what I enter into the custom fields for Hometown or Branch.
// register query vars
function sm_register_query_vars( $vars ) {
$vars[] = 'hometown';
$vars[] = 'branch';
return $vars;
}
add_filter( 'query_vars', 'sm_register_query_vars' );
// pre get posts
function sm_pre_get_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() ){
return;
}
if ( !is_post_type_archive( 'veterans' ) ){
return;
}
$meta_query = array();
// add meta_query elements
if( !empty( get_query_var( 'hometown' ) ) ){
$meta_query[] = array( 'key' => 'hometown', 'value' => get_query_var( 'hometown' ), 'compare' => 'LIKE' );
}
if( !empty( get_query_var( 'branch' ) ) ){
$meta_query[] = array( 'key' => 'branch', 'value' => get_query_var( 'branch' ), 'compare' => 'LIKE' );
}
if( count( $meta_query ) > 1 ){
$meta_query['relation'] = 'AND';
}
if( count( $meta_query ) > 0 ){
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'sm_pre_get_posts', 1 );
// the search form (display via shortcode)
function sm_search_form( $args ){
$output = '<form id="smform" action="' . esc_url( home_url() ) . '" method="GET" role="search">';
$output .= '<div class="smtextfield"><input type="text" name="s" placeholder="Name" value="' . get_search_query() . '" /></div>';
$output .= '<div class="smtextfield"><input type="text" name="hometown" placeholder="Hometown" value="' . get_search_query() . '" /></div>';
$output .= '<div class="smtextfield"><input type="text" name="branch" placeholder="Branch" value="' . get_search_query() . '" /></div>';
$output .= '<input type="hidden" name="post_type" value="veterans" />';
$output .= '<p><input type="submit" value="Search" class="button" /></p></form>';
return $output;
}
The above query looks like this when attempting to search:
site.com/?s=john+doe&branch=army&hometown=new+york&post_type=veterans
After checking if there are any terms in your search by something like
if($_GET['myfield'])...
try storing each type of your search in a var and then build a custom query with all the items that are in your search. ie :
<?php
$title=$_GET['name']; // Get the name
$params=[]; // Create an array with all the parameters you've got except the name
function populate_array($term) // Create a function to populate your array
{
if ($_GET[$term]) {
$params[$term] = $_GET[$term];
}
}
populate_array('hometown');
populate_array('branch');
//(...)
$args=array( // Initialize your query
'post_type' => 'my_post_type', // Just set your post type if needed
);
if($title){ // If there is a title add it to the query
$args['title']=$title;
}
if(count($params)>0){. // If there are any params
$meta=array('relation'=>'AND'); // Because you asked for it
foreach($params as $param => $value){
$meta[]=array( // Adding each meta tou your query
'key' => $param, // considering you name your meta as your parameter
'value' => $value,
'compare' => '='
);
}
$args['meta_query']=$meta; // Adding your meta to your main query
}
$query = new WP_Query( $args ); // And now you can request your query with all your parameters
I have not tested this but it should work... Maybe some improvements are needed. Test it and come back to me :)
That code work only set a variable not by form.
function.php
<?php
function title_filter( $where, &$wp_query ) {
global $wpdb;
if ( $search_term = $wp_query->get( 'search_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
}
return $where;
}
add_filter( 'posts_where', 'title_filter', 10, 2 );
?>
form.php
<form role="search" method="get" id="form_percorsi" action="<?php bloginfo('url'); ?>/category/percorsi/" style="float:left;margin-right:10px;">
<h1>Search</h1>
<input placeholder="Cerca…" value="" id="search_them" name="search_title" title="Search for:" type="search">
</form>
my query for display items
$search_them = $_GET['search_them'];
$args = array( 'paged' => $paged, 'orderby' => $order_by, 'order' => $order, 'search_title' => $search_them );
if i search a title by form non work...
have a solution?
This is my code for the widget:
function form( $instance ) {
$instance = wp_parse_args( (array)$instance, array(
'checkboxes' => array(
'Monday' => array('name' => 'Monday', 'value' => 'Monday', 'checked' => 1),
'Tuesday' => array('name' => 'Tuesday', 'value' => 'Tuesday', 'checked' => ''),
'Wednesday' => array('name' => 'Wednesday', 'value' => 'Wednesday', 'checked' => ''),
'Thursday' => array('name' => 'Thursday', 'value' => 'Thursday', 'checked' => ''),
'Friday' => array('name' => 'Friday', 'value' => 'Friday', 'checked' => ''),
'Saturday' => array('name' => 'Saturday', 'value' => 'Saturday', 'checked' => ''),
'Sunday' => array('name' => 'Sunday', 'value' => 'Sunday', 'checked' => '')
),
'title' => 'Workdays'
));
include( plugin_dir_path(__FILE__) . '/views/admin.php' );
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['checkboxes'] = strip_tags($new_instance['checkboxes']);
return $instance;
}
This is the code for the view:
<div class='ws-business-info'>
<div class='form-group'>
<?php foreach($instance['checkboxes'] as $day ) : ?>
<div class='checkbox'>
<label>
<input type="checkbox"
name="<?php echo $day['name']; ?>"
class="form-control"
id="<?php echo $this->get_field_id($day['name']); ?>"
value="<?php echo $day['value']; ?>"
<?php checked('1', $day['checked']); ?>/>
<?php echo $day['name']; ?>
</label>
</div>
<?php endforeach; ?>
</div>
The widget displays the checkboxes as expected but the state will not save. Dumping the $old_instance variable in the update function gives a null value.
In the view file you defined input's id corectly, but you did not the same with name attribute.
Define the name attribute like this:
<input type="checkbox"
name="<?php echo $this->get_field_name( 'checkboxes' ), '[', esc_attr( $day['name'] ), ']'; ?>"
class="form-control"
id="<?php echo $this->get_field_id($day['name']); ?>"
value="<?php echo $day['value']; ?>"
<?php checked('1', $day['checked']); ?>/>
<?php echo $day['name']; ?>
public function widget( $args, $instance ) {
// outputs the content of the widget
}
This function has to be added along with the default constructor. Else the widget will not work.
Not exactly what you are looking for but this is how I saved a checkbox in metabox, you may get some hint from this...
Code used to display html
function html_rtsocial_metabox()
{
global $post;
$custom = get_post_custom($post->ID);
$suppress = $custom['_suppress_rtsocial'][0];
$checked = ($suppress=='yes')?'checked':'';
$value = ($suppress=='yes')?'yes':'no';
$html = '<br><input type="checkbox" name="_suppress_rtsocial" value="'.$value.'" '.$checked.' id="_suppress_rtsocial" /> Hide Social Icons ???';
$html .= '<br/><br/>';
echo $html;
}
While saving
update_post_meta($post_id,'_suppress_rtsocial',$_POST['_suppress_rtsocial']);
Added js for admin interface
function checkbox_helper(){
var ele =jQuery('#_suppress_rtsocial'),value;
ele.click(function(){
value = ele.is(':checked')?'yes':'no';
ele.val(value);
}
);
}