How to order WP_Query to group results?

If your naming system is consistent then simply ordering by the post name should do it: $args = array( ‘orderby’ => ‘title’, ‘order’ => ‘ASC’, ); $q = new WP_Query($args); var_dump($q->request); var_dump(wp_list_pluck($q->posts,’post_title’)); Or, as a pre_get_posts filter (assuming the main query is what we want to alter): add_action( ‘pre_get_posts’, function ($qry) { if ($qry->is_main_query()) { … Read more

how to use pre_gets_posts to exclude one queried ID from homepage loop

Couple of issues here You custom query should be inside your conditional statement Use get_posts which only returns the $posts property wp_reset_query() is used with query_posts which you must never ever use No need to the $post global Your code should look something like this add_action( ‘pre_get_posts’, ‘cdbz_modify_main_query’ ); function cdbz_modify_main_query( $query ) { if … Read more

Check for the main query from the template

Use in_the_loop(), it returns true if we are in the main loop, false if we are not, like on a custom query loop Return: (bool) True if caller is within loop, false if loop hasn’t started or ended. The difference between is_main_query() and in_the_loop() is that is_main_query() uses WP_Query::is_main_query() which in turn uses the global … Read more

meta_query: check if number exists

I believe, more robust solution would be to allow multiple values of custom field ‘participants’. You can store like following add_post_meta($post_id,’participants’,’value1′); add_post_meta($post_id,’participants’,’value2′); add_post_meta($post_id,’participants’,’value3′); add_post_meta($post_id,’participants’,’value4′); Deletion would be like delete_post_meta($post_id,’participants’,’value1′); Query will then change to a simpler one. ‘meta_query’ => array( array( ‘key’ => ‘participants’, ‘value’ => $user_id, ‘compare’ => ‘=’, ), ) In case you … Read more

Rewrite gets completely ignored

Your rerite rule is incorrect. It should be (assuming download is name or slug of the page): add_rewrite_rule(‘^download/([^/]+)/?$’, ‘index.php?pagename=download&file=$matches[1]’, ‘top’); Also, you need to declare the file query var. A sample and working code: function createRewriteRules() { add_rewrite_tag(‘%file%’, ‘([^&]+)’); add_rewrite_rule(‘^download/([^/]+)/?$’, ‘index.php?pagename=download&file=$matches[1]’, ‘top’); } add_action(‘init’, ‘createRewriteRules’); Note: Don’t forget to flush rewrite rules after adding new … Read more

Advanced WP_Query with meta_query, orderby?

You should really read the documentation on meta_query‘s Here is what is allowed in a meta_query key (string) – Custom field key. value (string|array) – Custom field value. It can be an array only when compare is ‘IN’, ‘NOT IN’, ‘BETWEEN’, or ‘NOT BETWEEN’. You don’t have to specify a value when using the ‘EXISTS’ … Read more

WP_Query posts with comments only

If you’d like to change the SQL query for a WP_Query, you’re going to need to use the posts_join filter, or in your case, it’s easier to go for a simple posts_where: function my_has_comments_filter( $where ) { $where .= ‘ AND comment_count > 0 ‘; return $where; } add_filter( ‘posts_where’, ‘my_has_comments_filter’ ); $query = new … Read more