Order wp_query by calculated field

Using WP_Query, you can order the results by meta_value_num: $args = array ( ‘post_type’ => ‘clinica’, ‘meta_key’ => ‘distance_field_name’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’ );

WP_Query min and max values

Depending on the way your script is recording max_price, it isn’t really a meta_key/meta_value on postmeta table. Did you try changin max_price clause to something like this? [‘max_price’] => [ ‘relation’ => ‘OR’, [ ‘key’ => ‘max_price’, ‘value’ => 11, ‘compare’ => ‘<=’, ], [ ‘key’ => ‘max_price’, ‘compare’ => ‘NOT EXISTS’, ], ],

Overriding The Loop with filter or hook

This seems to work, but I would be interested in hearing from others if it’s the “correct” way to provide this sort of override. I’m assuming you’re visiting a category archive page, like http://www.example.com/category/computers/. // landing page = 188 // category = “computers” (#8) function custom_wpquery( $query ){ // the main query global $wp_the_query; if … Read more

Display recent posts from the same category as current post in sidebar

Try this // Get categories $categories = wp_get_post_terms( get_the_ID(), ‘category’); // Check if there are any categories if( ! empty( $categories ) ) : // Get all posts within current category, but exclude current post $category_posts = new WP_Query( array( ‘cat’ => $categories[0]->term_id, ‘post__not_in’ => array( get_the_ID() ), ) ); // Check if there are … Read more

How to order posts tag by tag?

Like Eugene mentioned in his answer you need to run a query for each tag. I would create a foreach loop that went through each tag then queried the latest 2 posts from each. $tags = get_tags(); foreach ( $tags as $tag ) { echo ‘<h3>’ .$tag->name. ‘</h3>’; $tag_query = new WP_Query( array( ‘tag_id’ => … Read more

author.php with ACF and CPTs

REWORKED APPROACH The issue with the original answer is that although we pass post ID’s to post__in parameter, being on the author page, the main query removes all posts that does not belong to the author. We can pass an empty string to author_name via the pre_get_posts filter, but that inherintly breaks the query object, … Read more

Query all posts where meta value is empty

I think you forgot about the inherit post status. The default one in WP_Query is publish. You should also use = instead of LIKE, to avoid using LIKE ‘%%’ in the SQL query. So try to add this: ‘post_status’ => ‘inherit’ and ‘compare’ => ‘=’ into your query arguments, to match the empty _wp_attachment_image_alt string … Read more

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