• Resolved MisterUser

    (@misteruser)


    On my site, each post is an event that can have many dates, and for each date there is an associated time. I am storing the pair in meta_value with the string ‘YYYY-MM-DD @ HH:MM’. I am able to store the string, however I cannot figure out how to make a query that will find posts with the meta_value that starts with a certain date. For instance, if I want to find all events on 2012-02-18, then I should use a mysql WHERE meta_value = ‘2012-02-18%’, right? However, I can’t get any mysql searches to work. I’ve tried direct MYSQL scripts, I’ve tried get_row, get_posts, get_results. Nothing works. Can anyone help me write a function that returns the array of objects like get_posts that contain the posts associated with these meta_values? Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    This should work with the format you have:

    $args = array(
       'meta_key' => 'date',
       'orderby' => 'meta_value',
    );
    query_posts($args); // or use get_posts($args);

    or this:

    <?php
    // change $post_id and 'date'
    $meta_date = get_post_meta($post_id, 'date', true);
    list($date, $time) = explode('@',$meta_date);
    
    $args = array(
       'meta_key' => trim($date),
       'orderby' => 'meta_value',
    );
    query_posts($args); // or use get_posts($args);
    ?>

    Thread Starter MisterUser

    (@misteruser)

    Thank you for your response. The first part is great, because that will help me once I get the post and can therefore get the post’s ID.

    However the second part of the code won’t work. The meta_key value is ‘show_date’, but I want to search for the meta_value that actually contains the date. For instance, when I type in:

    $args = array(
        'meta_value' => '2012-02-04 @ 8',
        );
        $result = query_posts($args);

    It returns the event that has that exact string in the meta_value. I don’t want that. I want to be able to type in:

    $args = array(
        'meta_value' => '2012-02-04%',
        );
        $result = query_posts($args);

    So that I can get all posts whose meta_value STARTS with that date. And I’m not even using a string. I’m passing the date through a variable $d, so I would need to use $d% or something in the above example for the meta_value. How do I do a partial match like that?

    Thread Starter MisterUser

    (@misteruser)

    I finally figured it out! I used:

    $dt = $d.'%';
    $sql = "SELECT * FROM $this->mainTable WHERE meta_value LIKE '$dt' ";
    $result = $this->db->get_results($sql);

    I tried this many times, but I finally figured out that I had some quotes in the wrong places.(For instance, don’t put quotes around the table name, but evidently you should put them around the variable name.) This returns the ID of the post that contains that meta_value that starts with the date. Now I will try to use the code you gave me, keesiemeijer, to extract the dates and times.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘database query meta_value partial’ is closed to new replies.