Hi,
thanks, the article you linked to helps mainly with the [adverts_add] shortcode, how to use the created custom fields in the [adverts_list] search you can read here https://wpadverts.com/documentation/custom-fields-search-form/
Thx for answer. I try, but it still don’t work for me. Can you check please.
<?php
/**
Plugin Name: WPAdverts Snippets - Search by checkbox
Version: 1.0
Author: Greg Winiarski
Description: Adds categories dropdown input to [adverts_list] search bar.
*/
// The code below you can paste in your theme functions.php or create
// new plugin and paste the code there.
add_filter( 'adverts_form_load', 'search_by_neworused_form_load' );
add_filter( 'adverts_list_query', 'search_by_neworused_query' );
/**
* Adds category dropdown into search form in [adverts_list].
*
* @param array $form Search form scheme
* @return array Customized search form scheme
*/
function search_by_neworused_form_load( $form ) {
if( $form['name'] != 'search' ) {
return $form;
}
$form['field'][] = array(
"name" => "my_custom_neworused",
"type" => "adverts_field_checkbox",
"order" => 20,
"label" => "Its new? or used?",
"max_choices" => 1,
"options" => array(
array("value"=>"new", "text"=>"new"),
array("value"=>"used", "text"=>"used"),
),
"options_callback" => "adverts_taxonomies",
"meta" => array(
"search_group" => "visible",
"search_type" => "full"
)
);
return $form;
}
/**
* Adds tax_query param to WP_Query
*
* The tax_query is added only if it is in $_GET['advert_category']
*
* @param array $args WP_Query args
* @return array Modified WP_Query args
*/
function search_by_neworused_query( $args ) {
if( ! adverts_request( "my_custom_neworused" ) ) {
return $args;
}
$args["tax_query"] = array(
array(
'taxonomy' => 'my_custom_neworused',
'field' => 'term_id',
'terms' => adverts_request( "my_custom_neworused" ),
),
);
return $args;
}
I suppose that you have a separate code for [adverts_add] which adds field named my_custom_neworused to the form?
If so then to have the search working correctly you would need to replace the code
$args["tax_query"] = array(
array(
'taxonomy' => 'my_custom_neworused',
'field' => 'term_id',
'terms' => adverts_request( "my_custom_neworused" ),
),
);
with
$args["meta_query"][] = array(
'key' => 'my_custom_neworused',
'value' => adverts_request( "my_custom_neworused" ),
);
as most likely your custom field value is being saved as meta field not as a custom taxonomy.
It works, thank you I appreciate it. your plugin is really cool!!! 5stars
Thanks :), i am glad you are liking the plugin so far.