How to add custom query filters in WP_User_Query

The pre_user_query action hook in WordPress will allow you alter the SQL statements in the WP_User_Query object before they are run against the database. Note, this is an action, not a filter, so there’s no need to return the $user_query that gets passed in. add_action( ‘pre_user_query’, ‘add_my_custom_queries’ ); function add_my_custom_queries( $user_query ) { $user_query->query_fields .= … Read more

Pagination with 5 posts per page

This is working for me on a single page using a template. Just be sure to set next_posts_link( ‘Older Entries »’, $the_query->max_num_pages );. // set the “paged” parameter (use ‘page’ if the query is on a static front page) $paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : ‘1’; $args = array … Read more

ACF datepicker meta_query Compare Dates in m/d/Y g:i a – Not in Ymd Format

There should not be any need to do this. Even if an ACF Field is using ‘return_format’ => ‘m/d/Y g:i a’, The post_meta value is in YYYY-MM-DD 00:00:00 format. $date_now = date(‘Y-m-d’); $args = [ ‘meta_key’=>’the_date’, ‘meta_value’=>$date_now.’ 00:00:00′, ‘meta_compare’=>’>=’, ]; $query = new WP_Query( $args ); Edit: I’ve noticed some discrepancies in this, the value … Read more

Use WP_Query with a custom SQL query

You won’t be able to translate a complicated SQL query directly to a WP_Query. What you can do is return Post IDs from the SQL and pass those IDs into the WP_Query args. Do note that this is highly inefficient and will likely create slow queries as post__in is not fast. You need to modify … Read more

Loop through all tags & output posts in alphabetical list

(Untested) but should works with any taxonomy including the ‘tag’ taxonomy (post_tag). The following example uses the taxonomy with name ‘my-taxonomy’. <?php //Get terms for this taxonomy – orders by name ASC by default $terms = get_terms(‘my-taxonomy’); //Loop through each term foreach($terms as $term): //Query posts by term. $args = array( ‘orderby’ => ‘title’, //As … Read more

How to know which one is the main query?

WP_Query->is_main_query() method source (which function of same name calls) is very short and simple: function is_main_query() { global $wp_the_query; return $wp_the_query === $this; } The main query is the query that is stored in $wp_the_query global. But what is that global? When WP sets up main query it stores it in two places: $wp_the_query and … Read more

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