Grow your CSS skills. Land your dream job.

Custom WordPress Query for Custom Post Types

  • # July 24, 2012 at 12:36 pm

    I have been working on my site, and I have added a new beer section to it. It is currently working here, http://drewclardy.com/beers, but I would like to make some changes to that styling.

    I would like to order it in this format.

    [Country]

    [Company]
    [Beer]

    I am not super advanced at PHP or WordPress, but I was able to get the current setup with working with WP_Query. Does anyone have any ideas on how to get this to work. I am trying this code, and I can get it to list the Country and then the beer. It does not list the Company though. I think I am doing something wrong with the looping.

    < ?php // List posts by a Custom Field's values
    $meta_key = 'brewing_country'; // The meta_key of the Custom Field
    $sql = "
    SELECT p.*,m.meta_value
    FROM $wpdb->posts p
    LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
    WHERE p.post_type = 'beer'
    AND p.post_status = 'publish'
    AND m.meta_key = '$meta_key'
    ORDER BY m.meta_value, p.post_date DESC
    ";
    $meta_key2 = 'brewing_company'; // The meta_key of the Custom Field
    $sql2 = "
    SELECT p.*,m.meta_value
    FROM $wpdb->posts p
    LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
    WHERE p.post_type = 'beer'
    AND p.post_status = 'publish'
    AND m.meta_key = '$meta_key2'
    ORDER BY m.meta_value, p.post_date DESC
    ";
    $rows = $wpdb->get_results($sql);
    $rows2 = $wpdb->get_results($sql2);
    if ($rows) {
    foreach ($rows as $post) {
    setup_postdata($post);
    if ($post->meta_value != $current_value) {
    echo "$post->meta_value";
    $current_value = $post->meta_value;
    }
    the_title();
    }
    }
    ?>
    # July 24, 2012 at 12:42 pm

    I’m still pretty basic at using wordpress custom posts myself, but I think the reason you’re not seeing anything for the company is because you’re storing the company metadata in the variable $rows2 = $wpdb->get_results($sql2); but you’re not actually outputting anything from the $rows2 variable. You’re only looping through $rows . I’m still learning php myself, but I’m sure there’s an easier way to grab all that information too. I’ll let some guru try and help you with that though.

    Let me know if that helps!

    # July 24, 2012 at 12:46 pm

    Well, I had removed that part of it because it was not working. That was my attempt at ordering them by that field. I just copied the first section and tried to paste it into the first foreach loop.

    # July 24, 2012 at 12:51 pm

    If you do a var_dump() on $rows and $rows2, are you getting all the information that you want? If you could show me what that outputs I might be able to help you loop through it.

    In case you’ve never used var_dump() before:

    var_dump($rows);
    # July 24, 2012 at 1:01 pm

    Here is a var_dump($rows).

    http://pastebin.com/GcRVK58N

    # July 24, 2012 at 1:03 pm

    Here is a var_dump($rows2).

    http://pastebin.com/XkuGGztT

    The data is exactly the same really. I just need to get that somehow formatted into that foreach statement.

    # July 24, 2012 at 1:12 pm

    Also, here is the code that I am trying to get working.

    < ?php // List posts by a Custom Field's values
    $meta_key = 'brewing_country'; // The meta_key of the Custom Field
    $sql = "
    SELECT p.*,m.meta_value
    FROM $wpdb->posts p
    LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
    WHERE p.post_type = 'beer'
    AND p.post_status = 'publish'
    AND m.meta_key = '$meta_key'
    ORDER BY m.meta_value, p.post_date DESC
    ";
    $meta_key2 = 'brewing_company'; // The meta_key of the Custom Field
    $sql2 = "
    SELECT p.*,m.meta_value
    FROM $wpdb->posts p
    LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
    WHERE p.post_type = 'beer'
    AND p.post_status = 'publish'
    AND m.meta_key = '$meta_key2'
    ORDER BY m.meta_value, p.post_date DESC
    ";
    $rows = $wpdb->get_results($sql);
    $rows2 = $wpdb->get_results($sql2);
    if ($rows) {
    foreach ($rows as $post) {
    setup_postdata($post);
    if ($post->meta_value != $current_value) {
    echo "

    $post->meta_value

    ";
    $current_value = $post->meta_value;
    if ($rows2) {
    foreach ($rows2 as $post2) {
    setup_postdata($post2);
    if ($post2->meta_value != $current_value2) {
    echo "

    $post2->meta_value

    ";
    $current_value2 = $post->meta_value;
    }
    the_title();
    }
    }
    }
    }
    }


    ?>
    # July 24, 2012 at 1:13 pm

    This produces an error on the page.

    Notice: Undefined variable: current_value in /var/www/test.drewclardy.com/public/wp-content/themes/drewclardy/content-beer-test.php on line 54

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".