Wordpress restrict categories plugin with DWQA plugin - wordpress

My environment:
using DWQA plugin and customized restrict categories plugin to work with DWQA plugin
Problem:
I have customized "restrict categories" plugin to work for DWQA instead of WP posts.
Now the problem is when I logged in as user with privileges set to category2 from restrict categories, it's not able to show anything for me.
To set category filter restrict categories is using the following code:
add_filter( 'pre_get_posts', array( &$this, 'posts_query' ) );
public function posts_query( $query ){
if ( $this->cat_list !== '' ) {
// Build an array for the categories
$cat_list_array = explode( ',', $this->cat_list );
// Make sure the posts are removed by default or if filter category is ran
if ( ! isset( $_REQUEST['cat'] ) )
$query->set( 'category__in', $cat_list_array );
elseif( isset( $_REQUEST['cat'] ) && $_REQUEST['cat'] == '0' )
$query->set( 'category__in', $cat_list_array );
}
return $query;
}
If I use category__not_in in place of category__in then all the posts are showing.

I have resolved this by changing code as below
public function posts_query( $query ){
// Build an array for the categories
$cat_list_array = explode( ',', $this->cat_list );
$taxquery = array(
array(
'taxonomy' => 'dwqa-question_category',
'field' => 'id',
'terms' => $cat_list_array,
'operator'=> 'NOT IN'
)
);
$query->set( 'tax_query', $taxquery );

Related

WooCommerce product filter for an attribute in the backend

I want to have a filter for an attribute "brand" in the WooCommerce products section in the backend.
I am using the following code:
function custom_woocommerce_product_filters( $output ) {
global $wp_query;
$output .= wc_product_dropdown_categories(
array(
'show_option_none' => 'Filter by brand',
'taxonomy' => 'pa_brand',
'name' => 'pa_brand',
'selected' => isset( $wp_query->query_vars['pa_brand'] ) ? $wp_query->query_vars['pa_brand'] : ''
)
);
return $output;
}
add_filter( 'woocommerce_product_filters', 'custom_woocommerce_product_filters' );
This code displays the dropdown list for the filter with all brands inside it, but clicking on "Filter" does not return the filtered products. It simply shows all the products.
Can someone help please?
Thanks
I recently discovered that you have to actually filter the results using the pre_get_posts() filter, having the dropdown alone is not enough. What I was able to come up with after searching similar questions on stackoverflow:
// Filter woocommerce admin products by artist
function apply_artist_custom_product_filters( $query ) {
global $pagenow;
// Ensure it is an edit.php admin page, the filter exists and has a value, and that it's the products page
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['pa_artist'] ) && $_GET['pa_artist'] != '' && $_GET['post_type'] == 'product' ) {
// Create meta query array and add to WP_Query
$meta_key_query = array(
array(
'taxonomy' => 'pa_artist',
'field' => 'slug',
'terms' => esc_attr( $_GET['pa_artist'] ),
'operator' => 'IN',
)
);
$query->set( 'tax_query', $meta_key_query );
}
}
add_action( 'pre_get_posts', 'apply_artist_custom_product_filters' );
The snippet that did most of the work is found here:WooCommerce: Adding a custom filter to the Product Admin area. I only changed the meta query to be a tax query. Not sure if it's the best way to do it, but it works for me. What I can't figure out is the "selected" part of the wc_product_dropdown_categories, for me when I select an artist and hit filter, the filter works but it doesn't save the selection.

Customizing Woocommerce Loop Query

I want to display my woocommerce products on two different sections on the site i.e. the shop page and on an archive page i created using Custom Post UI plugin, called artists.
Using the Advanced Custom Fields plugin, i created a field that will attach each product created to an individual artist. The plan now is, on each artist page, pull products for the artist.
This is what i have so far. I hook into woocommerce_product_query, check if i'm on the single artist page and implement my meta query:
function artist_products( $query )
{
if( is_singular('artists') )
{
$meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];
$meta_query[] = array(
'key' => '_fld_select_artist',
'value' => get_the_ID(),
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'woocommerce_product_query', 'artist_products' );
This is not working. When i visit my single artist page, i get a 500 server error. What I'm i missing?
woocommerce_product_query was breaking my site for some reason so i switched to pre_get_posts:
function artist_products( $query )
{
if ( $query->get('post_type') == 'nav_menu_item' )
{
return $query;
}
if( ! is_admin() && is_singular('artists') )
{
$meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];
$meta_query[] = array(
'key' => '_fld_select_artist',
'value' => get_the_ID(),
'compare' => '=',
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'artist_products' );

Woocommerce URL to hide out of stock product

I create a catalog website using Woocommerce to display all the product. When the product is sell, i dont remove it from the website (because we dont have a lot of product and we want to show to the customer what we sell before).
So, when you go on "All the products" you see the Sell products and the products available. I want, on the sidebar create a button "Show only available product". I dont find a plugin who can do this..
Whis woocommerce, can i create a URL like "mywebsite.com/products&instock=true" for example or something like this ? or if you know another solution. Thanks
You can use pre_get_posts to achieve it. Set the tax_query to not get the term outofstock of the product_visibility taxonomy.
In addition to my code, you will of course need to create a link with the prefix_instock=true parameter. You also can store it in a cookie, so it will be easily persistent.
add_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
function prefix_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() || empty($_GET['prefix_instock'])) {
return;
}
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) && $_GET['prefix_instock'] == 'true') {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
}

List all posts without at least one category assigned to them in admin

I am trying to allow the admin user to list out CPT that have not already had a category assigned to them (This is to enable them to quickly find posts that require a category and edit them)
I have this so far -
function function_name( $query ) {
global $post_type;
if ( is_admin() && $post_type == 'product' ) {
$query->set( 'cat', '' );
}
}
add_action( 'pre_get_posts', 'function_name' );
This does not work however. Can someone help point me in the right direction?
Thanks
I have achieved what I need this way, if anyone else comes up with this question -
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
$arr = array();
foreach ($terms as $term) {
$arr[] = '-'.$term->term_id;
}
$query->set( 'cat', $arr );
I am simply getting all category id's. looping through these and appending them to another array with a munis "-" value and passing them through to the query as an argument.

Woocommerce product search not checking "product_tag"?

It appears as tho the search functionality for WooCommerce products does not check "product_tag" taxonomy terms, nor SKU field? I added the SKUs as product tags to their respective products, but it still returns nothing when I search for the SKU.... How do I make the search functionality check product_tag terms? I have tried many many many things from adding tax_query to pre_get_post filter, to a whole new WP_Query loop, it just fails to search product_tags for some reason....so what is the point in a Product Tag???
I've tried the code from woo and others to search SKU with no results. However, the plugin search-everything, download from repository, does the job.
I got the solution to search the products by SKU code.
It is too simple just paste the following code in function.php and then you can see the changes in search results.
function search_by_id_only( $search, &$query_vars ) {
global $wpdb, $pagenow;
if ( 'edit.php' == $pagenow || empty($search) ) {
return $search;
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $query_vars->query['s'],
'compare' => 'LIKE'
)
)
);
$posts = get_posts( $args );
if ( empty( $posts ) ) return $search;
$get_post_ids = array();
foreach($posts as $post){
$get_post_ids[] = $post->ID;
}
if ( sizeof( $get_post_ids ) > 0 ) {
$search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
}
return $search;
}
add_filter( 'posts_search', 'search_by_id_only', 999, 2 );

Resources