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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:39:39+00:00 2026-06-02T18:39:39+00:00

i am working on a project that displays posts type within a radius entered

  • 0

i am working on a project that displays posts type within a radius entered ordered by the distance from the target address.
right now i am using this query which works fine:

sql = " SELECT SQL_CALC_FOUND_ROWS DISTINCT wposts.post_id, ( " . $_GET['mikm'] . " *           acos( cos( radians( $lat ) ) * cos( radians( wposts.lat ) ) * cos( radians( wposts.long ) - radians( $long ) ) + sin( radians( $lat ) ) * sin( radians( wposts.lat) ) ) )  AS distance  
    FROM post_address  wposts 
    WHERE wposts.post_type ='" . $post_type . "'
    HAVING distance <= $radius OR distance IS NULL ORDER BY distance LIMIT " . $from_page . "," . $per_page;

post_address is a table that hold posts id, post type, and addresses. when a post being saved or updated with an address this table is being update with the information.
the form i use has fields for address, checkbox to choose mile or kilometers, and distance in a dropdown menu. i use the method get with the form for pagination.

while the query above works fine i am trying to add taxonomies support. i managed to make it work with one taxonomy using

wp_dropdown_categories($tax_name)

and the SQL:

SELECT SQL_CALC_FOUND_ROWS DISTINCT wposts.post_id, ( " . $_GET['mikm'] . " * acos( cos( radians( $lat ) ) * cos( radians( wposts.lat ) ) * cos( radians( wposts.long ) - radians( $long ) ) + sin( radians( $lat ) ) * sin( radians( wposts.lat) ) ) )  AS distance  
FROM post_address  wposts
LEFT JOIN $wpdb->term_relationships ON (wposts.post_id = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE wposts.post_type ='" . $post_type . "'
AND $wpdb->term_taxonomy.taxonomy IN ('" $tax_name "')
AND ($wpdb->term_taxonomy.parent IN (" . $_GET[$tax_name] . ") OR $wpdb->term_taxonomy.term_id IN (" . $_GET[$tax_name] . "))
HAVING distance <= $radius OR distance IS NULL ORDER BY distance LIMIT " . $from_page . "," . $per_page;

now, since one taxonomy work fine i want to make it work with multiple taxonomies and that is where i get stuck. if for example post type “cars” has 2 taxonomies “model” and “color” i want to be able to filted posts that has “this” model and “this” color. i do not know how many taxonomies exist for the post type(who ever going to use this plugin will enter it manually in admin settings) and they right now being saved in array. for example
$taxonomies = array(‘car_model’,’car_color’) and i can populate the categories dropdown using:

`
foreach ($taxonomies as $tax) {
echo     '<div id="' . $tax . '_cat">';
echo       '<label for="category-id">Choose category: </label>';
        custom_taxonomy_dropdown($tax); 
echo     '</div>';    
`

and

function custom_taxonomy_dropdown($tax_name) {
$args = array(
        'taxonomy'          => $tax_name,
        'hide_empty'        => 0,
        'depth'             => 10,
        'hierarchical'      =>  1,
        'id'                =>  $tax_name . '_id',
        'name'              => $tax_name,
        'selected'          => $_GET[$tax_name],
        'show_option_all'   => 'All categories',
        );      
wp_dropdown_categories($args); 
} 

the dropdowns works fine and the output being sent to url as well but i cant find out the SQL query that will make it work.

this is my first project. excuse me if this question is way to long but i was trying to as clear as i can. i also not sure if any of the code above are the best way to go but any help from anyone would be much appreciated.

UPDATE:

below is the code i am using now and it works with multiple taxonomies. however, i still get into some issues. when i am choosing “all categories” in all of the taxonomies the sql will look like:

AND $wpdb->term_taxonomy.term_id IN () 

and the count will show count(*) = 0 which bring no results instead of all results.

other issue is with child and grandchild categories. when i choose the parent category i expect to get results of all its child and grandchild categories. but since no post is attached to the parent but to its child categories i get no results.
this is my code now:

    "SELECT SQL_CALC_FOUND_ROWS DISTINCT wposts.post_id, (" . $_GET['mikm'] . "* acos( cos( radians( $lat ) ) * cos( radians( wposts.lat ) ) * cos( radians( wposts.long ) - radians( $long ) ) + sin( radians( $lat ) ) * sin( radians( wposts.lat) ) ) )  AS distance 
        FROM posts_address  wposts
        LEFT JOIN $wpdb->term_relationships ON (wposts.post_id = $wpdb->term_relationships.object_id)
        LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
        LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE 
    wposts.post_type IN ('" . $post_type . "')

    AND $wpdb->term_taxonomy.term_id IN (" . $total_terms .") 
    GROUP BY wposts.post_id, wposts.lat, wposts.long        
        HAVING count(*) = " . $bc . " AND distance <= $radius OR distance IS NULL ORDER BY distance LIMIT " . $from_page . "," . $per_page  ;

any help is appriciated.

  • 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-02T18:39:42+00:00Added an answer on June 2, 2026 at 6:39 pm

    If I read your requirements correctly, you don’t need to extract information on the taxonomy from the database, only verify that a post “joins” all required taxonomies. Join posts with all required taxonomies, then filter based on the line multiplication result.

    First step, where you have

    $wpdb->term_taxonomy.taxonomy IN ('" $tax_name "')
    

    and

    $wpdb->term_taxonomy.parent IN (" . $_GET[$tax_name] . ") OR $wpdb->term_taxonomy.term_id IN (" . $_GET[$tax_name] . "))
    

    have it so that the arguments that go into the IN operator produce a list of all the taxonomy names, comma separated, surrounded by quotes. e.g:

    $wpdb->term_taxonomy.taxonomy IN ('model_a', 'color_blue')
    

    Now, the query will multiply posts with all of the taxonomies (in your filter) that the post matches. From here on, it’s simple: group by the post fields (you need only group by the ones you output in the query), and filter using HAVING so that you return only posts multiplied by all categories. In this example, the post should have matched two categories, so:

    (...)
    GROUP BY wposts.post_id, wposts.lat, wposts.long
    HAVING count(*) = 2 AND (distance <= $radius OR distance IS NULL ORDER BY distance LIMIT " . $from_page . "," . $per_page);
    

    Note that the count(*) filter should match the number of taxonomy “axes” matched. Also note that I’m assuming there are no collisions in taxonomies (i.e. There is no “blue” model and “blue” color).

    You should probably remove the DISTINCT operator in the query (grouping already produces distinct posts, and I’m not sure when will mysql’s optimizer calculate the distinct operator).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an iOS project that loads UIImages from URLs in a
I am creating a WinForm project that displays messages from a server and a
I am working on an Android app that displays photos which are downloaded from
I'm working on a project that screen scrapes a list of departure times from
I'm working on a project right now that is a shoutbox. Since I have
I am currently working on a project that dynamically displays DB content into table.
I have been working on a project that displays data in an XML file.
I'm working on a C# XNA project that requires me to display information based
I'm working on project that lets users choose some scientific authors and columnists and
Im working on a project that has an implementation of JOSSO in place. We

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.