Forums

Give help. Get help.

  • # 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 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 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag