Custom search in wordpress for all and custom post both - wordpress

I want option for all search and custom post type in search bar . I have done for custom post search but when i search for all then comes error and not working.Please see image and code.
Searching is working for both custom post type. But when i search for all then its showing error. Please see below my code.
function mySearchFilter($query){$post_type = $_GET['searchfrom'];
if($post_type != 'all')
{
if ($query->is_search)
{
if (!empty($post_type)) {
$query->set('post_type', $post_type);
}
}
}
else
{
$query->set();
}
return $query;}
add_filter('pre_get_posts','mySearchFilter');
Please let me know about the else condition.

function mySearchFilter($query){
$post_type = $_GET['searchfrom'];
if($post_type != 'all')
{
if ($query->is_search())
{
if (!empty($post_type)) {
$query->set('post_type', $post_type);
}
}
}
return $query;
}
add_filter('pre_get_posts','mySearchFilter');

Related

WooCommerce WC()->session->set not working

I want to add some of the fields from a form into custom fields on a product so I can use them into an API call after payment is made. I have a redirection on my form that gets to the checkout page and add the product in the basket, code below:
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
if (jQuery("input[type='checkbox'][name='try[]']").is(':checked')) {
location = 'http://www.thelittlegym.co.za/checkout/?add-to-cart=2506';
}
}, false );
</script>
Then, I have some code in my function.php to save the form data in the WC session.
I had to add declare the new session otherwise the set function didn't work.
If I do the init then I get an error saying "wc_empty_cart();" isn't defined (this function is from the WooCommerce plugin.
add_action('wpcf7_before_send_mail', 'send_form_data_to_wc_session', 5, 1);
function send_form_data_to_wc_session($contact_form) {
$submission = WPCF7_Submission::get_instance();
if($submission) {
$posted_data = $submission->get_posted_data();
if (!empty($posted_data['try'][0])) {
// Set data to WooCommerce session
WC()->session = new WC_Session_Handler();
// WC()->session->init();
WC()->session->set('cf7_posted_data', $posted_data);
}
}
}
Finally, I'm trying to retrieve the session data with the code below.
The var dump just returns NULL.
add_action('woocommerce_checkout_before_customer_details', 'wc_save_cf7_data_to_order', 10, 1);
function wc_save_cf7_data_to_order($order_id) {
$posted_data = WC()->session->get('cf7_posted_data');
var_dump($posted_data);
if(!empty($posted_data)) {
foreach($posted_data as $key => $data) {
echo '<b>', $key, ' : </b> ', $data, '<br />';
}
WC()->session->__unset('cf7_posted_data');
}
}
I believe the WC session set isn't working. I'm looking in the console for the Session Storage and can only see "wc_cart_hash_xxxxxxx", "wc_fragment_xxxxxxx" and "wc_cart_created".
Any idea how I can go ahead to debug this?
I've found an alternative solution on another topic, which solved my problem even if it doesn't explain why the code above didn't work.
add_action('wpcf7_before_send_mail', 'send_form_data_to_wc_session', 5, 1);
function send_form_data_to_wc_session($contact_form) {
$submission = WPCF7_Submission::get_instance();
if($submission) {
$posted_data = $submission->get_posted_data();
if (!empty($posted_data['try'][0])) {
// Set data to Session
session_start();//place this at the top of all code
$_SESSION['cf7_posted_data']=$posted_data;
}
}
}
and to retrieve:
add_action('woocommerce_checkout_before_customer_details', 'wc_save_cf7_data_to_order', 10, 1);
function wc_save_cf7_data_to_order($order_id) {
session_start();
$posted_data = $_SESSION['cf7_posted_data'];
var_dump($posted_data);
}

Wordpress Display ACF only in first depth nav

I write function for display acf in menu.
I need to display only in depth [0].
All work. But i still to se terrible notice:
Notice: Undefined index: nav_menu_item_depth in
here is my code:
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types($choices)
{
$choices['Menu']['menu_level'] = 'Livello Menu';
return $choices;
}
add_filter('acf/location/rule_values/menu_level', 'acf_location_rule_values_level');
function acf_location_rule_values_level($choices)
{
$choices[0] = '0';
$choices[1] = '1';
return $choices;
}
add_filter('acf/location/rule_match/menu_level', 'acf_location_rule_match_level', 10, 4);
function acf_location_rule_match_level($match, $rule, $options, $field_group)
{
global $current_screen;
if ($current_screen->id == 'nav-menus') {
if ($rule ['operator'] == "==") {
$match = ($options['nav_menu_item_depth'] == $rule['value']); // <-- Problem is here
}
}
return $match;
}
Some can help me to understand?
Thanks
Ciao Paolo! It worked for me by adding an isset() check on $options['nav_menu_item_depth']. My guess is that this runs on the whole nav-menus page before hitting the nav-items.
function acf_location_rule_match_level($match, $rule, $options, $field_group)
{
global $current_screen;
if ($current_screen->id == 'nav-menus' && isset($options['nav_menu_item_depth'])) {
if ($rule ['operator'] == "==") {
$match = ($options['nav_menu_item_depth'] == $rule['value']); // <-- Problem is here
}
}
return $match;
}
I'm pretty new to WP so I'd love to hear if there is a better way to do this or if this is a bad idea for some reason.

How can i sure that mysql statements done susseccfully in wordpress

I added some field to wordpress commments and add below code for save them :
add_action ('comment_post', 'add_comment_bid_values', 1);
function add_comment_bid_values($comment_id) {
if(isset($_POST['bidprice'])) {
$bidprice = wp_filter_nohtml_kses($_POST['bidprice']);
add_comment_meta($comment_id, 'bidprice', $bidprice, false);
}
if(isset($_POST['bidday'])) {
$bidday = wp_filter_nohtml_kses($_POST['bidday']);
add_comment_meta($comment_id, 'bidday', $bidday, false);
}
if(isset($_POST['bidprepay'])) {
$bidprepay = wp_filter_nohtml_kses($_POST['bidprepay']);
add_comment_meta($comment_id, 'bidprepay', $bidprepay, false);
}
if(isset($_POST['bidsponsor'])) {
$bidsponsor = wp_filter_nohtml_kses($_POST['bidsponsor']);
add_comment_meta($comment_id, 'bidsponsor', $bidsponsor, false);
}
if(isset($_POST['bidfetured'])) {
$bidfetured = wp_filter_nohtml_kses($_POST['bidfetured']);
add_comment_meta($comment_id, 'bidfetured', $bidfetured, false);
}
}
Now
How i can sure that comment and it's meta inserted into wordpress database, because some information save by comment_form function and this meta save by above code and also wordpress don't have transaction .
Thanks
As wp code reference says - 'comment_post' action hook is fired immediately AFTER a comment is inserted into the database. That means your code will be executed only if comment successfully inserted in database. For comment meta checking you can do something like that:
$result = add_comment_meta($comment_id, 'your_meta_key', $yourMetaValue, false)
//add_comment_meta will return false on error
if (false === $result) {
//do something here for example wp_die( __('Comment meta error', 'textdomain') );
}

How to pass an element in Crawler each function?

I parse my xml with Symfony's Crawler and cannot get how can I pass (other words continue) an element and not to include it into final array?
For example:
$node->filterXPath('//ExampleNode')->each(function(Crawler $child, $i) {
if (! count($child->filterXPath('//ChildNode'))) {
continue;
}
return $child->filterXPath('//ChildNode')->text();
});
You can use the Symfony\Component\DomCrawler\Crawler::reduce(Closure)
$crawler->reduce(function($result, $item) {
$childNodes = $item->filterXPath('//ChildNode');
if ($childNodes->count()) {
$result[] = $item;
}
});

Drupal - assign menu to block based on node type

I want to assign a specific menu in left sidebar block, based on the node type of the page currently being displayed. I think it should look something like this, but I am stuck.
function my_module_nodeapi(&$node, $op) {
switch ($op) {
case 'view':
if ($node->type == "large_reptiles")
{
//menu_set_active_menu_name('menu_reptile_menu');
//menu_set_active_item('menu_reptile_menu');
}
break;
}
}
You can't use hook_nodeapi for that. You should instead create the block yourself in a module and based on the node print the menu.
function hook_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'view':
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
}
if (!empty($node) && node->type == '...') {
// Theme the menu you want
}
...
else {
// Provide a default option
}
....
}
}

Resources