CPT Archive pre_get_posts not working?

&& is_post_type_archive(‘local-attractions’) is_post_type_archive isn’t set yet, WP is still deciding this, hence the call to pre _get_posts. Instead use: && $query->is_post_type_archive(‘local-attractions’) The same is true of any other is_ type methods, use the $query object, else you might be asking these questions about a completely different query and getting confusing results

Custom tax_query filter not working for Woocommerce product categories

I figured out what the issue was. The page was displaying categories/subcategories and not the items themselves. This meant that woocommerce_product_subcategories() was the one filtering and that uses a different hook. Here is what I added to my functions.php file. add_filter( ‘get_terms’, ‘get_subcategory_terms’, 10, 3 ); function get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = … Read more

pre_get_posts – editing query, tax_query

So there’s a few things wrong with the code: You’re not using the query object that’s passed to the pre_get_posts hook, so you’re not modifying the actual query. Field is set to id instead of term_id. If you’re using IN as the operator, pass an array to terms. You’re not sanitizing the value of $_POST[‘networks’]. … Read more

Modify date query by URL parameter using pre_get_posts and $_GET

I’m not where I can test this, but for something like: www.website.com/events?date=past, perhaps an approach like that outlined below could work. (I can almost guarantee a type or two, so be sure to rewrite/edit/rework this into your function and ensure you don’t inherit my errors). if (isset($_GET[‘date’])) { //using past, present, future as example. not … Read more

get_posts() interrupt because of filter

Check that the current query is the main query using $query->is_main_query(). add_action( ‘pre_get_posts’, array($this, ‘exclude_category’) ); public function exclude_category( $query){ if( is_admin() && $query->is_main_query() && isset( $_GET[‘cta_filter’]) && ! empty( $_GET[‘cta_filter’] ) ) { $term = sanitize_text_field( $_GET[‘cta_filter’] ); $tax_query = $query->get(‘tax_query’) ?: array(); $tax_query[] = array( ‘taxonomy’ => ‘cta_tax’, ‘field’ => ‘slug’, ‘terms’ => … Read more

pre_get_posts with multiple queries

You need to have two queries, one handling the listings, the other handling the map. Since you need pagination for the listings, I’d suggest you use pre_get_posts for that query, so that you can use WordPress’ default pagination out of the box. For the map, create a new WP_Query in your archive template: $args = … Read more