Get the terms from a custom taxonomy without posts - wordpress

What I am asking is, is there a built-in function that gets the the result of the following mysql query? get_terms() seems to get only the terms that are assigned e.g. to a post and not the whole list.
select
name
from
wordpress.wp_terms
where
term_id in (select
term_id
FROM
wordpress.wp_term_taxonomy
where
taxonomy = 'custome_taxonomy')

I think you have a misunderstanding about get_terms and don't know the purpose of the arguments within get_terms
By default, all terms without posts are hidden, here is the argument
'hide_empty' => true,
To retrieve terms without posts, you'll need to pass false to the hide_empty argument, like this
$terms = get_terms( 'hide_empty=false' );

Related

How to get all featured sellers with dokan

Is it possible to get all featured sellesrs with dokan (wordpress plugin)?
I found only this option, which gets all sellers, but in returned data, there is no mention if the store is featured or not.
$sellers = dokan_get_sellers();
foreach ($sellers['users'] as $seller) {
$store_info = dokan_get_store_info($seller->ID);
}
Next time you want to know exactly what a functions does and how it does whatever it does, just follow the term. Searching for dokan_get_sellers on all files of the plugin takes us to where the function is written and there we see we can pass an array of arguments to it. One of the arguments it accepts is "featured" and it expects a yes or no for value. So, this is what we can do:
$args = array( 'featured' => 'yes' );
$sellers = dokan_get_sellers( $args );
I did not test it, but it should be right.

How can I force a WordPress Query to satisfy ALL requirements?

I have 2 categories, called red and blue.
I have 2 posts, called red-post and blue-post. These posts are of their corresponding categories.
If I create the following query:
$bluePostQuery = new WP_Query(array('category' => 'red', 'name' => 'blue-post'));
This query will return blue-post, even though my query has category=red as a requirement. Why does this happen? blue-post is of the category blue, not red, so I want nothing returned here. Will a query always return if the name argument is provided, even if the other arguments are not met?
It may be ignoring 'category' because that is not a valid parameter for finding a category in WP Query. Here's a list of the valid params for cats. You might try using the category name parameter, like so:
$bluePostQuery = new WP_Query(
array(
'category_name' => 'red',
'name' => 'blue-post'
)
);
Alternatively, if you are searcing for a specific post, and know its slug, the category doesn't really matter, so you could just exclude it.

Filtering WooCommerce Products by $_GET

What I need to be able to do is apply a value for an attribute in the URL, so only the products match that attribute are displayed. I don't want to do any widgets or other visible filters on the page.
For this I presume I would need to use one of the webhooks, and filtering out all products that are about to be displayed.
Can anyone advise which hook will be best in this case and a simple explanation on how the triggered function will return the new array of products?
Thanks in advance!
NB: I also want to query a custom attribute, which does not have any terms, just a straight key/value.
UPDATE 1
I'm playing with two techniques; one is very reliable, and that's basically to use:
if (!$product->attributes || $product->get_attribute( 'testKey' ) != $_GET["testKey"]) {
//return;
}
at the top of content-product.php, but of course WooCommerce will still say the original value for found_posts. Certainly not ideal.
I've come across that something like this should work in functions.php:
function testFilter($meta_query) {
$meta_query[] = array (
'key' => 'testKey',
'value' => 'testVal',
'compare' => '='
);
return $meta_query;
}
add_filter( 'woocommerce_product_query_meta_query', 'testFilter', 9 );
Except it doesn't, returns no results, doesn't matter if I use LIKE, EXISTS etc. Am I using it wrong?
UPDATE 2
I'm not going to say this is the answer, as this only seems to look for one value within a group of custom attributes, but this result has helped.
add_filter( 'wpv_filter_query', 'wpv_filter_color_attribute' );
function wpv_filter_color_attribute( $query_args) {
$tax_query = array();
$tax_query['taxonomy'] = 'pa_size';
$tax_query['field'] = 'term_id';
$tax_query['terms'] = $_GET['pa_size'];
$query_args['tax_query'] = array($tax_query);
return $query_args;
}
You should replace "pa_size" with your attribute taxonomy slug, also $_GET['pa_size'] with the right URL parameter.
You can filter products by attributes using above code. I have not tried this. But, this may help you.

How to alphabetically sort custom posts if taxonomy is equals to 'custom_taxonomy'?

I'm trying to sort my custom posts alphabetically without touching any of the core files of the plugin.
I've tried the code below in functions.php and it works. BUT I want it to apply only to a certain custom taxonomy and/or post type.
function set_custom_post_types_order($wp_query) {
// 'orderby' value can be any column name
$wp_query->set('orderby', 'title');
// 'order' value can be ASC or DESC
$wp_query->set('order', 'ASC');
}
add_filter('pre_get_posts', 'set_custom_post_types_order');
I've also tried adding filter through it using "get_post_type" but it doesn't sort posts anymore.
note: it goes through the filter and display test var_dump in each post.
you can add conditions to check if its a taxonomy ( http://codex.wordpress.org/Conditional_Tags#A_Taxonomy_Page_.28and_related.29 ) and/or a specific post type ( http://codex.wordpress.org/Function_Reference/get_post_type, use your query object to get the correct ID ).

Wordpress: How to combine keyword search and meta filter in WP_query

I am trying to combine the search by keyword and meta_key and meta_value filter in WP_query. Keyword search and meta filter works good independently, but together are not. How to get it done? Thanks!
Here is my code:
if ( isset($_REQUEST['search']) AND $_REQUEST['search'] )
{
$query_param = array( 's' => urldecode($_REQUEST['search']), 'post_type' => GOODS_POST_TYPE );
$query_param['meta_key'] = 'sku';
$query_param['meta_value'] = urldecode($_REQUEST['search']);
}
$query = new WP_Query($query_param);
Modify your query before the search is run, and use Wordpress' built-in functions to do what you need rather than relying on unfiltered data:
<?php
add_action('pre_get_posts', 'modify_goods_search');
function modify_goods_search($query)
{
if($query->is_search)
{
$query->set('meta_key', 'sku');
$query->set('meta_value', get_search_query());
$query->set('post_type', GOODS_POST_TYPE);
}
}
?>
Remember that this will modify ALL searches. If you want to modify only one particular search, you can modify the conditional to make sure you're on a particular page before modifying the given query.
You might also want to consider using meta_query rather than just meta_key and meta_value.
Information on everything:
Pre Get Posts
Custom Field Parameters in Meta Query
Get Search Query

Resources