修改WordPress查询以在检索帖子时添加到计算字段中

Is there any way to modify the WordPress query to add in a calculated field? I want to order the post based on what is in the post title so that any posts that contains "GOLD" will appear first in random order, "SILVER" will appear next in random order and then all other posts.

The query would look like this:

SELECT *,
  IF(post_title LIKE '%GOLD%',FLOOR(RAND()*(999999-500000+1))+500000,post_title LIKE '%SILVER%',FLOOR(RAND()*(499999-1+1))+1,0)) AS post_order
  FROM wp_posts
  ORDER BY post_order DESC      

I looked at query_posts() to achieve this but it does appear to do what I would like.

You can run any SELECT query in WordPress using $wpdb->get_results. Note that $wpdb is a global variable in WordPress.

For example:

global $wpdb;
$your_query = "SELECT * FROM wp_posts";
$results = $wpdb->get_results($your_query);
foreach ($results as $post){
    // your loop output here
}

Reference: