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 8103665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:36:47+00:00 2026-06-05T23:36:47+00:00

I’m trying to create a custom SELECT query to find my Personal Best races

  • 0

I’m trying to create a custom SELECT query to find my Personal Best races from WordPress Custom Types where the times are a custom meta field.

This is the query I used to select the posts. I’ve simplified it for debugging, but eventually I want to select several term slugs and find the MIN for each using GROUP BY:

        SELECT      t.slug,
                    p.ID, 
                    p.post_date,
                    p.post_title,                    
                    m.meta_value as pb_mins
        FROM        $wpdb->posts p,
                    $wpdb->postmeta m,
                    $wpdb->term_relationships tr,
                    $wpdb->term_taxonomy tt,
                    $wpdb->terms t
        WHERE       p.ID = tr.object_id
                AND p.ID = m.post_id
                AND tr.term_taxonomy_id = tt.term_taxonomy_id
                AND tt.term_id = t.term_id
                AND p.post_type = 'runs'
                AND m.meta_key = 'ssr_duration_min'
                AND t.slug in ('10mi')

(I’m outputting the results to an array and using print_r to check it temporarily.)

This selects the two races/runs with the ’10mi’ term and displays the correct times/date/title for them.

The problem comes when I add the MIN function:

        SELECT      t.slug,
                    p.ID, 
                    p.post_date,
                    p.post_title,                    
                    MIN(m.meta_value) as pb_mins

The correct (minimum) time is selected, but the other details (date, title) are from the OTHER 10mi run/race.

I’ve tried changing the FROM clause to use LEFT JOIN and also INNER JOIN but the same thing happens: it’s fine for selecting both ’10mi’ runs but gives the wrong output when I add the MIN function.

Thanks for any help you can offer. This is my first post here so please let me know if I need to add any more details.

  • 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-06-05T23:36:49+00:00Added an answer on June 5, 2026 at 11:36 pm

    I never got round to posting my solution to this. It’s been a while now, so I can’t fully remember how I even arrived at it (most likely trial and error) but if this helps anyone, they’re welcome to it. If I think of any more details, I’ll add them as comments to this answer.

                SELECT      p1.ID,
                            m1.meta_value,
                            t1.name
                FROM        $wpdb->posts p1,
                            $wpdb->postmeta m1,
                            $wpdb->term_relationships tr1,
                            $wpdb->term_taxonomy tt1,
                            $wpdb->terms t1,
                            (   SELECT      t2.slug as pb_distance,
                                            MIN(m2.meta_value) as pb_duration
                                FROM        $wpdb->posts p2,
                                            $wpdb->postmeta m2,
                                            $wpdb->term_relationships tr2,
                                            $wpdb->term_taxonomy tt2,
                                            $wpdb->terms t2
                                WHERE       p2.ID = tr2.object_id
                                        AND p2.ID = m2.post_id
                                        AND tr2.term_taxonomy_id = tt2.term_taxonomy_id
                                        AND tt2.term_id = t2.term_id
                                        AND p2.post_type = 'runs'
                                        AND p2.post_status = 'publish'
                                        AND m2.meta_key = 'ssr_duration_min'
                                        AND t2.slug IN $slugs
                                GROUP BY    t2.slug ) pb
                WHERE       p1.ID = m1.post_id
                        AND p1.ID = tr1.object_id
                        AND tr1.term_taxonomy_id = tt1.term_taxonomy_id
                        AND tt1.term_id = t1.term_id
                        AND p1.post_type = 'runs'
                        AND p1.post_status = 'publish'
                        AND m1.meta_key = 'ssr_duration_min'
                        AND t1.slug in $slugs
                        AND m1.meta_value = pb.pb_duration
                ORDER BY    meta_value+0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.