I am trying create filter with AJAX which filter different taxonomy categories. For each taxonomy is filter listbox.
Filter is working if all listbox are chosen, but i need find way how filter categories if some of filter are not selected. How can query categories with empty value ? I have 4 listboxes, code is shortened.
Code:
$komanda = $_POST['komanda'];
$pretinieks = $_POST['pretinieks'];
if ($komanda == 0){ $komanda = '?'; }
if ($pretinieks == 0){ $pretinieks = '?'; }
$args = array(
'post_type' => 'speles',
'posts_per_page' => 99,
'post_status' => array('publish'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'komandas',
'field' => 'term_id',
'terms' => $komanda,
),
array(
'taxonomy' => 'pretinieki',
'field' => 'term_id',
'terms' => $pretinieks,
),
)
);
I found solution.
$komanda = $_POST['komanda'];
$pretinieks = $_POST['pretinieks'];
if ($komanda == 0){
$terms = get_terms( 'komandas', 'orderby=count&hide_empty=1' );
$komanda = array();
$komanda = wp_list_pluck($terms, 'term_id');
}
if ($pretinieks == 0){
$terms = get_terms( 'pretinieki', 'orderby=count&hide_empty=1' );
$pretinieks = array();
$pretinieks = wp_list_pluck($terms, 'term_id');
}
$args = array(
'post_type' => 'speles',
'posts_per_page' => 99,
'post_status' => array('publish'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'komandas',
'field' => 'term_id',
'terms' => $komanda,
),
array(
'taxonomy' => 'pretinieki',
'field' => 'term_id',
'terms' => $pretinieks,
),
)
);
I have area (lat0, lat1, lng0, lng1). Why my wp_query not working?
$new_query->query( array(
'post_type' => 'object',
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'lat',
'compare' => 'BETWEEN',
'value' => array($lat0, $lat1),
'type' => 'DECIMAL',
),
array(
'key' => 'lng',
'compare' => 'BETWEEN',
'value' => array($lng0, $lng1),
'type' => 'DECIMAL',
)
),
),
)
);
I get some result if I use:
array(
'key' => 'lat',
'compare' => '<=',
'value' => $lat0,
'type' => 'DECIMAL',
),
But this code not working:
array(
'relation' => 'AND',
array(
'key' => 'lat',
'compare' => '>=',
'value' => $lat0,
'type' => 'DECIMAL',
),
array(
'key' => 'lat',
'compare' => '<=',
'value' => $lat1,
'type' => 'DECIMAL',
)
),
Can I use wp_query for search by area. And if this wrong way, what I can do?
Fixed
global $wpdb;
$querystr = "
SELECT *
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta m1
ON ( $wpdb->posts.ID = m1.post_id )
INNER JOIN $wpdb->postmeta m2
ON ( $wpdb->posts.ID = m2.post_id )
INNER JOIN $wpdb->postmeta m3
ON ( $wpdb->posts.ID = m3.post_id )
WHERE
$wpdb->posts.post_type = 'object'
AND $wpdb->posts.post_status = 'publish'
AND ( m1.meta_key = 'lat' AND m1.meta_value < $lat0 )
AND ( m1.meta_key = 'lat' AND m1.meta_value > $lat1 )
AND ( m2.meta_key = 'lng' AND m2.meta_value < $lng0 )
AND ( m2.meta_key = 'lng' AND m2.meta_value > $lng1 )
AND ( m3.meta_key = 'city' AND m3.meta_value = $city_string )
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date
DESC;
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
I want to get minimum price and maximum price based on categories.
The woocommerce query which gives me minimum and maximum product price range but i want it on the bases of category.
for example: category="music,clothing".
Here is query for minimum and maximum price:
$min = floor( $wpdb->get_var(
$wpdb->prepare('
SELECT min(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price', '_min_variation_price' ) ) ) . '")
AND meta_value != ""
', $wpdb->posts, $wpdb->postmeta )
) );
$max = ceil( $wpdb->get_var(
$wpdb->prepare('
SELECT max(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
', $wpdb->posts, $wpdb->postmeta, '_price' )
) );
Please suggest me how can i get it according to category selected.
SQL Query:
//SQL Query to get max price :
SELECT max(meta_value + 0) FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE meta_key = '_price' AND (wp_term_relationships.term_taxonomy_id IN (46,47));
//SQL Query to get min price :
SELECT min(meta_value + 0) FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE (meta_key = '_price' OR meta_key='_min_variation_price') AND (wp_term_relationships.term_taxonomy_id IN (46,47));
//46, 47 is the term id of category. So you need to have id of music and clothing
WP_Query :
This is the way how I do it :
<?php
$category = array('t-shirt');
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
)
)
);
$loop = new WP_Query($args);
echo "Max :" get_post_meta($loop->posts[0]->ID, '_price', true);
?>
I used 'order'=>'DESC' so that it will sort by Highest to Lower and hence we can get Highest from it.
If you want to have Min, change 'order'=>'DESC' to 'order'=>'ASC' and you will have echo "Min :" get_post_meta($loop->posts[0]->ID, '_price', true);
Rohil_PHPBeginner's answer is correct, but you do no need to get prices for all products to find the maximum. You can just alter the "posts_per_page" to be 1:
<?php
$category = array('t-shirt');
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
)
)
);
$loop = new WP_Query($args);
echo "Max :" get_post_meta($loop->posts[0]->ID, '_price', true);
wp_reset_postdata();
?>
i want to create a default view from mymodule. i want to use fields from 2 tables.My base table is users. All the fields from usres are showing. i want the fields from draagabaleviews_structure also. i had a look on hook_views_data. draagabaleviews group is already there in view.then how can i join that table??
function mymodule_views_default_views() {
$view = new view;
$view->name = 'my_view';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'users';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE;
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
));
$handler->override_option('fields', array(
'name' => array(
'label' => 'Username',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'link_class' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'strip_tags' => 0,
'html' => 0,
),
'link_to_node' => 0,
'exclude' => 0,
'id' => 'name',
'table' => 'users',
'field' => 'name',
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
),
'status' => array(
'label' => 'Status',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'link_class' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'strip_tags' => 0,
'html' => 0,
),
'link_to_user' => 0,
'overwrite_anonymous' => 0,
'anonymous_text' => '',
'exclude' => 0,
'id' => 'status',
'table' => 'users',
'field' => 'status',
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
),
'Last access' => array(
'label' => 'Last access',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'link_class' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'strip_tags' => 0,
'html' => 0,
),
'link_to_user' => 0,
'overwrite_anonymous' => 0,
'anonymous_text' => '',
'exclude' => 0,
'id' => 'Last access',
'table' => 'users',
'field' => 'access',
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
),
'order' => array(
'label' => 'Order',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'link_class' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'strip_tags' => 0,
'html' => 0,
),
'set_precision' => FALSE,
'precision' => 0,
'decimal' => '.',
'separator' => ',',
'prefix' => '',
'suffix' => '',
'exclude' => 0,
'id' => 'order',
'table' => 'draggableviews_structure',
'field' => 'value',
'relationship' => 'none',
),
));
$handler->override_option('sorts', array(
'order' => array(
'order' => 'ASC',
'id' => 'Order',
'table' => 'draggableviews_structure',
'field' => 'value',
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
));
$handler->override_option('access', array(
'type' => 'perm',
'perm' => 'Allow Reordering',
));
$handler->override_option('cache', array(
'type' => 'none',
));
$handler->override_option('empty', 'This book page doesn\'t contain any sub pages.');
$handler->override_option('empty_format', '1');
$handler->override_option('items_per_page', 20);
$handler->override_option('use_pager', '1');
$handler->override_option('style_plugin', 'draggabletable');
$handler->override_option('style_options', array(
'override' => 1,
'sticky' => 0,
'order' => 'asc',
'columns' => array(
'title' => 'title',
'name' => 'name',
),
'info' => array(
'title' => array(
'sortable' => 0,
'separator' => '',
),
'name' => array(
'sortable' => 0,
'separator' => '',
),
),
'tabledrag_order' => array(
'field' => 'order',
'handler' => 'native',
),
'draggableviews_extensions' => array(
'extension_top' => '3',
'extension_bottom' => '3',
),
'tabledrag_order_visible' => array(
'visible' => 1,
),
'tabledrag_hierarchy_visible' => array(
'visible' => 0,
),
'draggableviews_depth_limit' => '0',
'draggableviews_repair' => array(
'repair' => 'repair',
),
'tabledrag_types_add' => 'Add type',
'tabledrag_expand' => array(
'expand_links' => 'expand_links',
'collapsed' => 0,
'by_draggableviews_structure' => 0,
),
'tabledrag_lock' => array(
'lock' => 0,
),
'draggableviews_default_on_top' => '1',
'draggableviews_button_text' => 'Save order',
'draggableviews_arguments' => array(
'use_args' => 0,
),
'draggableviews_book_radios' => '1',
'draggableviews_book_argument' => 'p',
'draggableviews_book_filter' => 'none',
));
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'user/reorder');
$handler->override_option('menu', array(
'type' => 'tab',
'title' => 'Rearrange',
'description' => 'Organize your book pages.',
'weight' => '10',
'name' => 'primary-links',
));
$handler->override_option('tab_options', array(
'type' => 'none',
'title' => '',
'description' => '',
'weight' => 0,
));
$views[$view->name] = $view;
return $views;
}
I was caught with the same issue where i needed to join the two table in view. fortunately i found these links
http://drupalmodules.com/module/reverse-node-reference module enhances views with reverse relationships for node reference fields.
you may also need http://drupal.org/project/noderelationships
These are contributed module. So if you want to do the same thing programmatically then you need to look into reverse-node-reference modules.