WordPress WP_Query custom order_by post_type functionality

You are right, in your current approach, WordPress does not natively support ordering by specific post types in WP_Query. However, you can still achieve what you’re looking for through a custom SQL clause. By using the posts_clauses filter, you can modify the SQL query before it’s executed. Please note that using custom SQL queries should … Read more

Showing all posts of the current custom taxonomy on archive page

I have finally found a solution and for anyone interested, here is the working code : <?php // The Query if (is_tax() || is_category() || is_tag() ){ $qobj = $wp_query->get_queried_object(); // concatenate the query $args = array( ‘post_type’ => ‘houses’, ‘posts_per_page’ => -1, ‘orderby’ => ‘title’, ‘order’ => ‘ASC’, ‘tax_query’ => array( array( ‘taxonomy’ => … Read more

How to `’orderby’ => ‘meta_value_num’` in a series of orderby parameters

The documentation for WP_Query indicates that ordering by meta value is limited to one meta key and requires the meta_key parameter (untested): $results = new WP_Query( array( ‘meta_key’ => ‘wpcf_catalogue_number’, ‘orderby’ => array( ‘meta_value_num’ => ‘ASC’, ), ) ); If you want additional sorting, you’ll need to sort the result ($results->posts) using PHP (untested): $sort__formats … Read more

WP_Query to find any published post with ID greater than given ID

If the date query doesn’t work because of modified post dates (e.g. if the posts’ timeline is no longer linear), here’s a posts_where filter’s suggestion (untested): add_filter( ‘posts_where’, function( $where, $query ) use ( &$wbdb ) { $id = $query->get( ‘_id__gt’ ); if( is_int( $id ) && $id > 0 ) { $where .= $wpdb->prepare( … Read more

Equivalent of url_to_postid() for non-post URLs?

The simplest solution is to get all the existing rewrite rules, match the requested URl to one of them, and then convert it into a query. Like so: // Get all the rewrite rules global $wp_rewrite; $url_path = “2020/04/test-post”; // Or “tag/whatever” or “feed/atom” etc // Match the URL against WordPress rewrite rules $rewrite_rules = … Read more

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