Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4123282
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:35:30+00:00 2026-05-20T23:35:30+00:00

My posts have a bunch of custom fields and are currently being ordered by

  • 0

My posts have a bunch of custom fields and are currently being ordered by “over-lenght”

I have another custom field called “over-lenghtin”.

I’d like to continue to order my posts by over-lenght but if 2 posts had the same value for this field I’d like to also order them by over-lengthin.

I’m wondering how I can modify my query or add a function to execute this?

Here’s by current query

$loop = new WP_Query( array ( 'post_type' => 'new', 'orderby' => 'meta_value_num', 'meta_key' => 'over-length', 'meta_query' => array( array( 'key' => 'over-make', 'value' => 'Doral', 'compare' => 'LIKE') ) ) );

Ok so I’ve rewritten by query to use $wpdb->get_results, but I’m still a bit confused on how to go about ordering by 2 different custom fields.

Here’s my new query:

$wpdb->get_results("SELECT * FROM $wpdb->posts, wp_postmeta  WHERE wp_posts.ID = wp_postmeta.post_id AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'new' AND wp_postmeta.meta_value = 'Doral' ORDER BY");

Now I need to order by my custom fields ‘over-length’ and then ‘over-lengthin’

I seem to have this working now, here’s my final query:

$wpdb->get_results("SELECT * FROM $wpdb->posts 
LEFT JOIN $wpdb->postmeta AS overlength ON(
$wpdb->posts.ID = overlength.post_id
AND overlength.meta_key = 'over-length'  
)
LEFT JOIN $wpdb->postmeta AS overlengthin ON(
$wpdb->posts.ID = overlengthin.post_id
AND overlengthin.meta_key = 'over-lengthin'  
)
LEFT JOIN $wpdb->postmeta AS overmake ON(
$wpdb->posts.ID = overmake.post_id
AND overmake.meta_key = 'over-make'  
)
WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'new' AND overmake.meta_value = 'Doral' ORDER BY 0+overlength.meta_value DESC, 0+overlengthin.meta_value  DESC");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:35:30+00:00Added an answer on May 20, 2026 at 11:35 pm

    Honestly, when it comes to more than one Custom Field, the easiest solution is to use $wpdb->get_results instead of WP_Query.

    You can get the query as it is currently being executed with

    echo $GLOBALS['wp_query']->request;
    

    Then just update the SQL and follow the steps to create the revised loop from here:

    • Displaying Posts Using a Custom Select Query | WordPress COdex
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of old posts on a blog that I would like
I have a bunch of posts, and I'd like to always show 3 per
I've seen a bunch of posts related to this, but none seem to have
I have a bunch of posts written in markdown and I need to remove
I have a bunch of Users, each of whom has many Posts. Schema: Users:
Let's say I have a bunch of posts and I want to know if
I have a HTML form that posts to PHP. I get a bunch of
I display a bunch of posts from users on a page. I have the
I have a custom built CRM that is 99% written in Classic ASP/VBScript. Currently
I have a bunch of Posts. I want that for the index action, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.