Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Custom WordPress Query for Custom Post Types

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39034
    dclardy
    Member

    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.

    
    $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();
    }
    }
    ?>
    #106692
    dclardy
    Member

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

    
    $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();
    }
    }
    }
    }
    }


    ?>
    #106693
    dclardy
    Member

    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)
  • The forum ‘Back End’ is closed to new topics and replies.