Sending variable to search page - wordpress

I`ve made this action:
add_action( 'init', 'advanced_search_query' );
function advanced_search_query( $query ) {
if ( isset($_GET['make']) && isset($_GET['model']) && isset($_GET['fuel']) ) {
$all_products = [];
$inner_cats = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $_GET['model']]);
foreach ($inner_cats as $cat) {
if ($_GET['fuel'] == 0)
$fuel_cat = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $cat->term_id, 'name' => 'Diesel']);
elseif ($_GET['fuel'] == 1)
$fuel_cat = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $cat->term_id, 'name' => 'Benzin']);
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $fuel_cat[0]->term_id,
'operator' => 'IN',
)
)
);
$products = get_posts( $args );
if ($products) {
foreach ($products as $prd) {
$all_products[] = $prd;
}
}
}
}
}
Now what I am strugling with is sending the $all_products to my search page and loop them. Tried to redirect to the search page like wp_safe_recirect('domain.com?s=""') with empty s parameter and use the all_results there. Also tried to set the variable as global but again couldnt reach it.

Did you try the following?
if ($products) {
foreach ($products as $prd) {
$all_products[] = $prd;
?>
<script>window.location.href="domain.com?s=''"</script>
<?php
}
}

Related

WooCommerce custom pagination is not fetching products according to post per page option value

WooCommerce pagination is not fetching products according to posts per page option value. It shows different products when click on pagination link, sometimes show correct number of products on click of pagination link but some time not. Same query is working for all custom pagination.
Here is my code:
public function render()
{
global $wp_query, $wp_rewrite;
$args= [];
$ordering_args =[];
$pagination_args =[];
$taxonomy = 'product_cat';
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => absint(get_option('posts_per_page')),
);
if (isset($_POST['orderby'])) {
if ('price-desc' === $_POST['orderby']) {
$args['orderby'] = 'price';
$args['order'] = 'DESC';
} elseif ('price' === $_POST['orderby']) {
$args['orderby'] = 'price';
$args['order'] = 'ASC';
} elseif ('date' === $_POST['orderby']) {
$args['orderby'] = 'date';
$args['order'] = 'DESC';
} else {
$args['orderby'] = $_POST['orderby'];
$args['order'] ='';
}
$ordering_args = WC()->query->get_catalog_ordering_args($args['orderby'], $args['order']);
$args['orderby'] = $ordering_args['orderby'];
$args['ordering_args'] = $ordering_args;
$args['order'] = $ordering_args['order'];
if ($ordering_args['meta_key']) {
$args['meta_key'] = $ordering_args['meta_key'];
}
}else{
$ordering_args = WC()->query->get_catalog_ordering_args();
$args['orderby'] = $ordering_args['orderby'];
$args['ordering_args'] = $ordering_args;
$args['order'] = $ordering_args['order'];
if ($ordering_args['meta_key']) {
$args['meta_key'] = $ordering_args['meta_key'];
}
}
if (isset($_POST['paged'])) {
$args['paged'] = absint($_POST['paged']);
if (1 < absint($_POST['paged'])) {
$args['paged'] = absint($_POST['paged']);
}
$pagination_args = array(
'total' => wc_get_loop_prop('total_pages'),
'current' =>absint($_POST['paged']),
);
$args['limit'] = isset($args['posts_per_page']) ? intval($args['posts_per_page']) : intval(get_option('posts_per_page'));
if (empty($args['offset'])) {
$args['offset'] = 1 < $args['paged'] ? ($args['paged'] - 1) * $args['limit'] : 0;
}
}
if (!empty($args['meta_query'])) {
if (!empty($_POST['rating'])) {
$args['meta_query']['relation'] ='AND';
$rating = $_POST['rating'];
$args['meta_query'][]= array(
'key' => '_wc_average_rating',
'value' => array( (int)$rating[0], (int)$rating[1] ),
'compare' => 'BETWEEN',
'type' => 'numeric'
);
}
} else {
if (!empty($_POST['rating'])) {
$rating = $_POST['rating'];
$args['meta_query']= array(
array(
'key' => '_wc_average_rating',
'value' => array( (int)$rating[0], (int)$rating[1] ),
'compare' => 'BETWEEN',
'type' => 'numeric'
)
);
}
}
if (!empty($_POST['product-cata'])) {
$category = $_POST['product-cata'];
$args['tax_query']= array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => (int)$category
)
);
}else{
if (!empty($_POST['category'])) {
$category = $_POST['category'];
$args['tax_query']= array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => (int)$category
)
);
}
}
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) {
ob_start();
// do_action('woocommerce_before_shop_loop');
woocommerce_product_loop_start();
while ($wp_query->have_posts()) {
$wp_query->the_post();
/**
* Hook: woocommerce_shop_loop.
*/
do_action('woocommerce_shop_loop');
echo wc_get_template_part('content', 'product');
}
woocommerce_product_loop_end();
//do_action('woocommerce_after_shop_loop');
wp_reset_postdata();
$_RESPONSE['posted_data'] =$_POST;
$_RESPONSE['products'] = ob_get_contents();
ob_end_clean();
if (isset($_POST['paged'])) {
$paged = (int) $_POST['paged'];
$base = $_POST['link'].'page/%#%';
$pagination_args = array(
'base' => $base,
'total' => (int)$wp_query->max_num_pages,
'per_page' => (int)get_option('posts_per_page'),
'current' => (int) max(1, $paged),
'format' => '',
);
$_RESPONSE['pagination'] = $this->custom_woocommerce_pagination($pagination_args);
$_RESPONSE['result_counts'] = $this->custom_result_count($pagination_args);
} else {
$base = $_POST['link'].'page/%#%';
$pagination_args = array(
'base' => $base,
'total' =>(int) $wp_query->max_num_pages,
'per_page' =>(int) get_option('posts_per_page'),
'current' => (int) max(1, 1),
'format' => '',
);
$_RESPONSE['pagination'] = $this->custom_woocommerce_pagination($pagination_args);
$_RESPONSE['result_counts'] = $this->custom_result_count($pagination_args);
}
$_RESPONSE['args'] = $args;
$_RESPONSE['pagination_args'] = $pagination_args;
} else {
ob_start();
do_action('woocommerce_no_products_found');
$_RESPONSE['products'] = ob_get_contents();
$_RESPONSE['args'] = $args;
ob_end_clean();
}
echo json_encode($_RESPONSE);
die();
}
Any help will appreciate. I am using this in ajax post method.
/*Display only in stock*/
$args['meta_query'][]= [
'key' => '_stock_status',
'value' => 'instock'
];
/*Display only visible product*/
$args['tax_query'][]= [
'taxonomy' => 'product_visibility',
'terms' => array( 'exclude-from-catalog' ),
'field' => 'name',
'operator' => 'NOT IN',
];

How can i display custom post type **posts** list in elementor select control

How can i display custom post type posts list in elementor select control
This is my code =>
I have used this but this shows post categories not post title
$options = array();
$posts = get_posts( array(
'post_type' => 'digital_card'
) );
foreach ( $posts as $key => $post ) {
setup_postdata( $post );
$options[$post->ID] = get_the_title();
}
$this->add_control(
'show_elements',
[
'label' => __( 'Select Posts', 'plugin-domain' ),
'label_block' => ('bool'),
'type' => \Elementor\Controls_Manager::SELECT,
'multiple' => true,
'options' => $options,
]
);
Try the below code.
$options = array();
$posts = get_posts( array(
'post_type' => 'digital_card'
) );
foreach ( $posts as $key => $post ) {
$options[$post->ID] = get_the_title($post->ID);
}
public static function getLocations()
{
$posts = get_posts(array('post_type' => 'location'));
$allPosts = array();
foreach ($posts as $key => $post) {
array_push(
$allPosts,
array(
'map_latitude' => $post->post_author,
'pin_title' => $post->post_title,
)
);
}
return $allPosts;
}
$this->add_control(
'id',
array(
'label' => __('Location Map Pins', 'textdomain'),
'type' => Controls_Manager::REPEATER,
'default' => Hip_Maps_Elementor_Widget::getLocations(),
'fields' => $repeater->get_controls(),
'title_field' => '{{{ pin_title }}}',
'prevent_empty' => false,
)
);

Filter with Advansed custom fields, Wordpress

I have filter in my website:
My filter form
Filtering algorithm is:
Filtering algorithm
PHP code for this algorithm:
$GLOBALS['my_query_filters'] = array(
'field_1' => 'work',
'field_2' => 'visa'
);
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
function my_pre_get_posts( $query ) {
if(is_admin()) return;
if(!$query->is_main_query()) return;
$meta_query = $query->get('meta_query');
$tax_query = $query->get('tax_query');
$k = 0;
foreach($GLOBALS['my_query_filters'] as $key => $name) {
if(empty($_GET[$name])) continue;
$value = explode(',', $_GET[$name]);
foreach($value as $item) {
$meta_query[$k] = array(
'key' => $name,
'value' => $item,
'type' => 'CHAR',
'compare' => 'LIKE'
);
$k++;
}
}
if($_GET['cat']) {
$tax_query[] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
'operator' => 'IN'
)
);
$query->set('tax_query', $tax_query);
}
$query->set('meta_query', $meta_query);
}
But I need change filtering algorithm to:
New filtering algorithm
I do not know much WP_Query(). Can you hellp me edit $meta_query?
I changed filter to:
function my_pre_get_posts( $query ) {
if(is_admin()) return;
if(!$query->is_main_query()) return;
$meta_query = $query->get('meta_query');
$tax_query = $query->get('tax_query');
foreach($GLOBALS['my_query_filters'] as $key => $name) {
if(empty($_GET[$name])) continue;
$meta_query['relation'] = 'AND';
$meta_query_inner['relation'] = 'OR';
$value = explode(',', $_GET[$name]);
$k = 0;
foreach($value as $item) {
$meta_query_inner[$k] = array(
'key' => $name,
'value' => $item,
'type' => 'CHAR',
'compare' => 'LIKE'
);
$k++;
}
$meta_query[] = $meta_query_inner;
}
if($_GET['cat']) {
$tax_query[] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
'operator' => 'IN'
)
);
$query->set('tax_query', $tax_query);
}
$query->set('meta_query', $meta_query);
}
It seems to work :)

Join 2 search queries in wordpress Ajax function

Hi I have tried everything to join the results of a common search with a sku search but nothing seems to work , it will either show the common search or the sku, not both, any help woulf be appreciated
if (!function_exists('yolo_result_search_callback')) {
function yolo_result_search_callback() {
ob_start();
$yolo_options = yolo_get_options();
$posts_per_page = 8;
if (isset($yolo_options['search_box_result_amount']) && !empty($yolo_options['search_box_result_amount'])) {
$posts_per_page = $yolo_options['search_box_result_amount'];
}
$post_type = array();
if (isset($yolo_options['search_box_post_type']) && is_array($yolo_options['search_box_post_type'])) {
foreach($yolo_options['search_box_post_type'] as $key => $value) {
if ($value == 1) {
$post_type[] = $key;
}
}
}
$keyword = $_REQUEST['keyword'];
if ( $keyword ) {
$search_query = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $keyword,
'posts_per_page' => $posts_per_page + 1,
'compare' => 'LIKE',
'relation' => 'OR',
)
)
);
$search_query = array(
's' => $keyword,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => $posts_per_page + 1,
);
$search = new WP_Query($search_query);
$newdata = array();
if ($search && count($search->post) > 0) {
$count = 0;
foreach ( $search->posts as $post ) {
if ($count == $posts_per_page) {
$newdata[] = array(
'id' => -2,
'title' => '<i class="wicon icon-outline-vector-icons-pack-94"></i> ' . esc_html__('View More','yolo-motor') . '',
'guid' => '',
'date' => null,
);
break;
}
$newdata[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'guid' => get_permalink( $post->ID ),
'date' => mysql2date( 'M d Y', $post->post_date ),
);
$count++;
}
}
else {
$newdata[] = array(
'id' => -1,
'title' => esc_html__( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'yolo-motor' ),
'guid' => '',
'date' => null,
);
}
ob_end_clean();
echo json_encode( $newdata );
}
die(); // this is required to return a proper result
}
add_action( 'wp_ajax_nopriv_result_search', 'yolo_result_search_callback' );
add_action( 'wp_ajax_result_search', 'yolo_result_search_callback' );
}
After playing around all day, I came up with the solution, maybe it can help someone else
if (!function_exists('yolo_result_search_callback')) {
function yolo_result_search_callback() {
ob_start();
$yolo_options = yolo_get_options();
$posts_per_page = 8;
if (isset($yolo_options['search_box_result_amount']) && !empty($yolo_options['search_box_result_amount'])) {
$posts_per_page = $yolo_options['search_box_result_amount'];
}
$post_type = array();
if (isset($yolo_options['search_box_post_type']) && is_array($yolo_options['search_box_post_type'])) {
foreach($yolo_options['search_box_post_type'] as $key => $value) {
if ($value == 1) {
$post_type[] = $key;
}
}
}
$keyword = $_REQUEST['keyword'];
if ( $keyword ) {
$search_query = array(
'post_type' => $post_type,
'meta_query' => array(
array(
'key' => '_sku',
'value' => $keyword,
'posts_per_page' => $posts_per_page + 1,
'compare' => 'LIKE',
'relation' => 'OR',
)
)
);
$search_query1 = array(
's' => $keyword,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => $posts_per_page + 1,
);
$search = new WP_Query($search_query);
$search1 = new WP_Query($search_query1);
$newdata = array();
if ($search && count($search->post) > 0) {
$count = 0;
foreach ( $search->posts as $post ) {
if ($count == $posts_per_page) {
$newdata[] = array(
'id' => -2,
'title' => '<i class="wicon icon-outline-vector-icons-pack-94"></i> ' . esc_html__('View More','yolo-motor') . '',
'guid' => '',
'date' => null,
);
break;
}
$newdata[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'guid' => get_permalink( $post->ID ),
'date' => mysql2date( 'M d Y', $post->post_date ),
);
$count++;
}
}
else {
if ($search1 && count($search1->post) > 0) {
$count = 0;
foreach ( $search1->posts as $post ) {
if ($count == $posts_per_page) {
$newdata[] = array(
'id' => -2,
'title' => '<i class="wicon icon-outline-vector-icons-pack-94"></i> ' . esc_html__('View More','yolo-motor') . '',
'guid' => '',
'date' => null,
);
break;
}
$newdata[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'guid' => get_permalink( $post->ID ),
'date' => mysql2date( 'M d Y', $post->post_date ),
);
$count++;
}
}
else {
$newdata[] = array(
'id' => -1,
'title' => esc_html__( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'yolo-motor' ),
'guid' => '',
'date' => null,
);
}
}
ob_end_clean();
echo json_encode( $newdata );
}
die(); // this is required to return a proper result
}
add_action( 'wp_ajax_nopriv_result_search', 'yolo_result_search_callback' );
add_action( 'wp_ajax_result_search', 'yolo_result_search_callback' );
}

woocommerce (arg) to get shop_order by product category

Currently I have a category called 'vou' and would like to just get shop_order under this category. I have tried many combinations and it's not working out. Thank you for reading:
$args = array(
'post_type' =>'shop_order',
'post_status' => array_keys( wc_get_order_statuses() ),
'posts_per_page' => -1,
'orderby'=> 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'vou'//,
//'terms' => get_terms( 'product_cat', array( 'fields' => 293 ) ),
)
)
);
How to get shop_order by product category 'vou'?
Find all your orders and gether orders that have required category. Make new args with post__in attribute.
$m_args_all = array(
'orderby' => 'date',
'posts_per_page' => 12,
);
$ordr_cat = 654 // id of required cat
$empty_cat = false;
$orders_all_raw = wc_get_orders( $m_args_all ); //find all orders by your args
if (!empty($orders_all_raw)) {
$orders_in_cat = [];
foreach ($orders_all_raw as $order_raw) {
$items = $order_raw->get_items(); //get products of order
if (!empty($items)) {
foreach ($items as $item_id => $item) { //loop for products
$term_ids = wp_get_post_terms($item->get_product_id(), 'product_cat', array('fields' => 'ids')); // get all id of cats in product
if (in_array($ordr_cat, $term_ids)) { // if required cat in array of gethered cats add order id to array
if (!in_array($order_raw->ID, $orders_in_cat)) {
$orders_in_cat[] = $order_raw->ID; //add order with required prod cat to array
}
}
}
}
}
if (empty($orders_in_cat)){
$empty_cat = true;
}
else {
$m_args_all += array('post__in' => $orders_in_cat);
}
}
if($empty_cat){
$orders = [];
}
else{
$orders = wc_get_orders( $m_args_all );
}

Resources