Hi,
the second code you pasted executes the search only, you also need a code that will display the checkbox in the [adverts_list] search form, the code below should do that
add_filter( 'adverts_form_load', 'search_by_custom_checkbox' );
function search_by_custom_checkbox( $form ) {
if( $form['name'] != 'search' ) {
return $form;
}
$form['field'][] = array(
"name" => "my_custom_checkbox",
"type" => "adverts_field_checkbox",
"order" => 20,
"label" => __("Category", "adverts"),
"max_choices" => 10,
"options" => array(
array("value"=>"1", "text"=>"One"),
array("value"=>"2", "text"=>"Two"),
),
"meta" => array(
"search_group" => "visible",
"search_type" => "full"
)
);
return $form;
}
Thread Starter
d2eck
(@d2eck)
Checkbox appears in add form, and in search form. But it does not search, when i set “One” or “Two”. May be i made mistake, all code is:
add_filter( "adverts_form_load", "my_adverts_form_load" );
function my_adverts_form_load( $form ) {
if( $form["name"] != "advert" ) {
return $form;
}
$form["field"][] = array(
"name" => "my_custom_checkbox",
"type" => "adverts_field_checkbox",
"order" => 2,
"label" => "Custom Checkbox",
"is_required" => false,
"validator" => array( ),
"max_choices" => 2,
"options" => array(
array("value"=>"1", "text"=>"One"),
array("value"=>"2", "text"=>"Two"),
)
);
return $form;
}
add_filter( 'adverts_list_query', 'search_by_custom_checkbox_query' );
function search_by_custom_checkbox_query( $args ) {
if( ! adverts_request( "my_custom_checkbox" ) ) {
return $args;
}
$args["meta_query"][] = array(
'key'=> "my_custom_checkbox",
'value'=> adverts_request( "my_custom_checkbox" ),
'compare'=> 'LIKE'
);
return $args;
}
add_filter( 'adverts_form_load', 'search_by_custom_checkbox' );
function search_by_custom_checkbox( $form ) {
if( $form['name'] != 'search' ) {
return $form;
}
$form['field'][] = array(
"name" => "my_custom_checkbox",
"type" => "adverts_field_checkbox",
"order" => 20,
"label" => __("Category", "adverts"),
"max_choices" => 10,
"options" => array(
array("value"=>"1", "text"=>"One"),
array("value"=>"2", "text"=>"Two"),
),
"meta" => array(
"search_group" => "visible",
"search_type" => "full"
)
);
return $form;
}
Thread Starter
d2eck
(@d2eck)
Oh, i found it, i replace ‘LIKE’ to ‘IN’! Now it works!
Last quastion, is there any way, to translate (i have multilanguage site) words “One”,”Two” and “Custom fields” LOCO translate does not see this words. (And other Lables that are written “by hand” in function.php)
Thanks, best regards!
Hi,
it is possible, but it will be rather complicated, if you have pasted this code in your theme functions.php file then you would need to change the phrase text"=>"One" to text"=>__("One", "your-theme-textdomain").
The your-theme-textdomain you need to replace with actual theme text-domain, you will find it in your theme style.css file, at the beginning of the file you should see something like “Text Domain: ….” the ‘….’ will be the text domain to use.
Once you do that, you can edit the translation for your theme using Loco Translate, click Sync button so it will find your new phrases and then translate them.