• Resolved joemastro

    (@joemastro)


    I understand custom post types, I understand basic querying of custom post types. I can’t seem to find an answer on quires beyond basic such as:

    Select * from custom post type
    Where condition 1 between x and y
    Condition 2 = abc
    Condition 3 = def

    Conditions 1 & 2 being custom tax. and 3 & 4 being being meta boxes. Those are not all the data pieces associated, but if I can figure out/learn a simple query as above, I can generally get going on the difficult ones.

    If I bang my head hard enough, I might be able to come up with a strait PHP solution… but that seems to defeat the purpose of using custom data types.

    As a side question… Would a strait PHP answer be more efficient? I am going to import 2,000 records into the database under this post type, and will add 1,000+ more every year.

    Thanks in advance for any help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • There is not enough detail here to allow anyone to provide an answer. Please provide the taxonomy names, taxonomy values, custom field names, and values for x, y, abc, and def.

    An example of what is in the database and the result you want would also help.

    Thread Starter joemastro

    (@joemastro)

    Thanks for answering.

    Select * from (custom post type = itinerary)
    Where tax_number_of_days between 7 and 14
    And tax_departure_date between ‘2012-03-05’ and ‘2012-04-05’
    And tax_tour_company = ‘Caravan Tours’
    Order By tax_departure_date

    Itinerary is the custom post type.
    Included in itinerary are meta boxes for number of days, date. Also included are the “categories” for a company, a tour type under the company, a main region, and regions broken down under the main region.
    The itinerary is in the content itself.

    company “A & K” tour type “river cruise”
    Region “Africa” sub-region “Nile River”

    When you say ‘meta boxes’, do you mean Custom Fields? If so, what are the Custom Field names?

    You put categories in quotes as if these were not real categories. If not, what are they? If they are categories, what ate the actual category names?

    Thread Starter joemastro

    (@joemastro)

    By “meta box” I mean the boxes/custom data fields created by :
    <?php add_meta_box( $id, $title, $callback, $page, $context, riority );

    Names are tour_date, num_days, additional_info

    Example of category names for companies, and the types of tours:
    “A & K”
    “Africa” with “Egypt”, “Victoria Falls”, South Africa, etc under it.
    “River Cruise”
    “Family Friendly”
    “Safari”
    “no kids”

    etc., etc..

    OK, I see how to query for the tour date and number of days:

    $sql = "
    SELECT p.*, m1.meta_value AS tourdate, m2.meta_value AS numdays, t.name AS tourcompany
    FROM $wpdb->posts p
    JOIN $wpdb->postmeta m1 ON (p.ID = m1.post_id AND m1.meta_key = 'tour_date')
    JOIN $wpdb->postmeta m2 ON (p.ID = m2.post_id AND m2.meta_key = 'num_days')
    JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
    JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category'
    JOIN $wpdb->terms t ON (tt.term_id = t.term_id AND t.name = 'Caravan Tours'
    WHERE p.post_type = 'itinerary'
    AND p.post_status = 'publish'
    AND m1.meta_value BETWEEN '2012-03-05' AND '2012-04-05'
    AND m2.meta_value BETWEEN 7 and 14
    ORDER BY m1.meta_value ASC";

    Since I have no way of testing this, there may be typos.

    Thread Starter joemastro

    (@joemastro)

    Sweet. I think I understand that. I’ll pop by your site to discuss it more when I get there. Now that I know I can query those posts, I will…. Now to merge the databases……

    Wow….. Adventure in FORTRAN. Put bird in cage. Something about a snake. that one?

    Hope the code works out.

    Colossal Cave had trolls, treasures, magic and mazes. “A ghostly voice says ‘Plugh’!”

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Advaanced quiries on custom data types.’ is closed to new replies.