pre_get_posts with get_posts

Firstly, you are invoking an infinite loop, which causes the memory exhaustion. To avoid it, put the following at the beginning of your function: // avoid infinite loop remove_action( ‘pre_get_posts’, __FUNCTION__ ); It makes sure the you are not hooking it into pre_get_posts over and over again, re-initiating your get_posts() call over and over again. … Read more

WP_Query for WooCommerce Products

<ul class=”products”> <?php $args = array( ‘post_type’ => ‘product’, ‘posts_per_page’ => 12 ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); wc_get_template_part( ‘content’, ‘product’ ); endwhile; } else { echo __( ‘No products found’ ); } wp_reset_postdata(); ?> </ul><!–/.products–>

Using WP_Query – how to display something IF there are no results

So, let’s say $query is your WP_Query object. I.e. $query = new WP_Query($some_query_args ); Then you can set up ‘the loop’, by $query->get_posts(); Then to check if there are actually any returned results: if ( $query->have_posts() ) : //Use a While loop to show the results else: //No results, let’s show a message instead. //This … Read more

wpdb::prepare was called incorrectly

It’s always advised to use $wpdb->prepare when you are taking input from user. This will help in protecting queries against SQL Injection. For more details, check the Codex When you use $wpdb->prepare, you must pass the variables to the query. In your case, you can skip using $wpdb->prepare as you are using a hard coded … Read more

Optimize Multiple Taxonomy Term MySQL Query?

While this is really a MySQL question it does help to understand the WordPress SQL schema and also I love trying to optimize SQL queries so rather than send you off to StackOverflow I’ll try to answer you here. You may still want to post it over there to get some other opinions. And while … Read more

How to search display_name column when using WP_User_Query

You can try this: /** * Add support for the “display_name” search column in WP_User_Query * * @see http://wordpress.stackexchange.com/a/166369/26350 */ add_filter( ‘user_search_columns’, function( $search_columns ) { $search_columns[] = ‘display_name’; return $search_columns; } ); where we use the user_search_columns filter to modify the available search columns. So what fields can we use with this filter? Here’s … Read more

Query WooCommerce orders where meta data does not exist

The meta_query argument (that you can use in a WP_Query) is not enabled by default when using a WC_Order_Query through wc_get_orders() WooCommerce function. But for you can use the undocumented Custom Field Parameters (just like in a WP_Query): meta_key meta_value meta_value_num meta_compare So in your case you can use the following instead: $orders = wc_get_orders( … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)