Check if any available product has specific attribute

Here is the code that solved the problem: $woo_args = array( ‘status’ => ‘publish’, ‘limit’ => 1, ‘stock_status’ => ‘instock’, ‘brand’ => 99 ); $products = wc_get_products($woo_args); if (count($products) == 0){ //No product with the custom attribute id=99 } add_filter(‘woocommerce_product_data_store_cpt_get_products_query’, ‘my_handle_custom_query_var’, 10, 2); function my_handle_custom_query_var($query, $query_vars) { if (!empty($query_vars[‘brand’])){ $query[‘tax_query’][] = array( ‘taxonomy’ => ‘pa_brand’, … Read more

Posts per Page on custom Taxonomy Template

number_posts is not a valid parameter in pre_get_posts, you should be using posts_per_page You should also include a check (!is_admin()) in your query to check whether you are on the front end or back end as pre_get_posts alters back end queries as well Rewrite your code to the following: add_action( ‘pre_get_posts’, function ( $query ) … Read more

When creating a new product, auto assign it to all custom taxonomy woocommerce

For post types not using the block editor, you can use wp_terms_checklist_args filter to manipulate the selected terms of a hierarchical taxonomy. You can use the filter to set pre-selected terms like so, function auto_check_hierarchial_terms_on_new_post( $args, $post_id ) { // is it new post? if ( ‘auto-draft’ !== get_post_status($post_id) ) { return $args; } // … Read more

Custom post type single page returning 404 error when sharing archive url with custom taxonomy

So after some more research I came across this https://stackoverflow.com/questions/23698827/custom-permalink-structure-custom-post-type-custom-taxonomy-post-name – which I now think I remember using at some point but must have deleted thinking it hadn’t worked (as I hadn’t figured out the rest of the process). But this is the particular function and filter that worked: /** * Tell WordPress how to … Read more

How can I write slugs / permalinks as: custom post type -> custom taxonomy and custom post type -> custom taxonomy (one CPT and many taxo)?

Thanks to Sally CJ, here is the solution that worked for me : /*To create new custom taxonomy */ function si0b_ct_year() { /* Property Type */ $labels = array( ‘name’ => _x(‘Years’, ‘Taxonomy General Name’, ‘siobanone’), ‘singular_name’ => _x(‘Year’, ‘Taxonomy Singular Name’, ‘siobanone’) ); $rewrite = array( ‘slug’ => ‘artistes’, ‘with_front’ => false, ‘hierarchical’ => … Read more