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

Display post from a date range from custom field

The magic parameter ist described at WP_Query under: Custom Field Parameters You need to add a meta_query array to your $args where you define your range. In your case the code could look like this: $range = array( ‘2015-05-01’, ‘2015-05-31’, ); $args = array( ‘post_type’ => ‘score_post_type’, ‘posts_per_page’ => 5, ‘meta_key’ => ‘score_time’, ‘orderby’ => … 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