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 be done cautiously, as it can lead to unexpected results or vulnerabilities if not handled properly.

Here’s a sample of how you could modify your query using this approach:

function custom_posts_orderby($orderby_statement) {
    $orderby_statement = "FIELD(post_type, 'reviews', 'articles', 'topics') ASC";
    return $orderby_statement;
}

add_filter('posts_orderby', 'custom_posts_orderby');

$query_posts_for_board_game = new WP_Query(
    array(
        'post_type' => ['reviews','articles', 'topics'],
        'posts_per_page' => 5,
        'meta_query' => array(
            // put your additional meta query here
        ),
    )
);

remove_filter('posts_orderby', 'custom_posts_orderby');

In this code, we use the posts_orderby filter to change the SQL ‘ORDER BY‘ clause so that it orders by the post_type field according to the order we specify in the FIELD function (‘reviews’ first, then ‘articles’, then ‘topics’). After the query is executed, we immediately remove the filter with remove_filter to ensure that it doesn’t affect other queries.

Make sure you thoroughly test the code, I didn’t test it on my end.

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casinocanlı bahis casino siteleriOnwin Güncel Girişholiganbetholiganbet girişholiganbet güncel giriş