I have created a custom post type in WordPress, and one of the fields is brewing company. I would like to be able to arrange the post by that field.
Meta field information:
Brewing Company = get_post_meta($post->ID, 'brewing_company', true);
Country = get_post_meta($post->ID, 'brewing_country', true);
Beer Name = the_title() of the post
Beer Type = get_post_meta($post->ID, 'brewing_style', true);
Something like this:
<ul>
<li>Brewing Company - Country</li>
<li>Beer Name, Beer Type</li>
<li>2 Beer Name, 2 Beer Type</li>
<li>2 Brewing Company - Country</li>
<li>Beer Name, Beer Type</li>
<li>2 Beer Name, 2 Beer Type</li>
</ul>
All of the fields are working properly, but I can't figure out how to create the correct loop. Here is my loop section for my beers page.
<?php
/**
* @package WordPress
* @subpackage Twenty Eleven
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php query_posts( get_post_meta( $post->ID, 'Query', true )); ?>
<?php if ( have_posts() ) : ?>
<ul>
<?php while ( have_posts() ) : the_post();
$brewing_company = get_post_meta($post->ID, 'brewing_company', true);
$brewing_country = get_post_meta($post->ID, 'brewing_country', true);
if ( $brewing_company != $brewing_company ){
echo '<li><span class="brewing-company">'. $brewing_company .'</span> , <span class="brewing-country">'. $brewing_country .'</span>';
}
?>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyeleven' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
I have been told that I can do this with taxonomies, but I have already placed all the effort in to do it with meta fields. I would like to continue on this path.
Luis Abarca answers:
<?php query_posts( get_post_meta( $post->ID, 'Query', true )); ?>
<?php if ( have_posts() ) : ?>
<ul>
<?php while ( have_posts() ) : the_post();
$brewing_company = get_post_meta($post->ID, 'brewing_company', true);
$brewing_country = get_post_meta($post->ID, 'brewing_country', true);
if ( $brewing_company != $brewing_company ){
echo '<li><span class="brewing-company">'. $brewing_company .'</span> , <span class="brewing-country">'. $brewing_country .'</span></li>';
?>
<li><span class="berr-name"><?php the_title() ?></span> , <span class="berr-type"><?php echo get_post_meta($post->ID, 'brewing_style', true) ?></span></li>
<?php
}
?>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query(); ?>
Drew Clardy comments:
That doesn't return anything after the initial content on the page. You can see it at http://drewclardy.com/beers.
Luis Abarca comments:
Im sorry, maybe i misunderstood the question, the content its a custom meta of the current post ?
Drew Clardy comments:
The page Beers has some content on it, and I would like to pull the custom post type beers that I have created. The custom post type has the meta data on it.
Luis Abarca comments:
Well as i understand, this is the code you use to call the custom posts ??
<?php query_posts( get_post_meta( $post->ID, 'Query', true )); ?>
But you are getting a plain value, i also suggest you to use WP_Query instead of query_posts.
<?php $beers = new WP_Query( ... ); ?>
<?php if ( $beers->have_posts() ) : ?>
<ul>
<?php while ( $beers->have_posts() ) : $beers->the_post(); ?>
// the rest of the loop
...
Drew Clardy comments:
So this is what I have changed the loop to at this time, but it is still not working.
<?php
/**
* @package WordPress
* @subpackage Twenty Eleven
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php $beers = new WP_Query('post_type=beer'); ?>
<?php if ( $beers->have_posts() ) : ?>
<ul>
<?php while ( $beers->have_posts() ) : $beers->the_post();
$brewing_company = get_post_meta($post->ID, 'brewing_company', true);
$brewing_country = get_post_meta($post->ID, 'brewing_country', true);
$brewing_style = get_post_meta($post->ID, 'brewing_style', true);
if ( $brewing_company != $brewing_company ){
echo '<li><span class="brewing-company">'. $brewing_company .'</span> , <span class="brewing-country">'. $brewing_country .'</span></li>'; ?>
<li><span class="beer-name"><?php the_title() ?></span> , <span class="beer-type"><?php echo $brewing_style ?> </span></li>
<?php
}
?>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyeleven' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
Drew Clardy comments:
Alright, I have it returning values now, but it is not grouping them by the manufacturer. How can I accomplish this?
<?php
/**
* @package WordPress
* @subpackage Twenty Eleven
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php $beers = new WP_Query( array( 'post_type' => 'beer', 'order' => 'ASC', 'showposts' => -1 ) ); ?>
<?php if ( $beers->have_posts() ) : ?>
<ul>
<?php while ( $beers->have_posts() ) : $beers->the_post();
$brewing_company = get_post_meta($beers->post->ID, 'brewing_company', true);
$brewing_country = get_post_meta($beers->post->ID, 'brewing_country', true);
$brewing_style = get_post_meta($beers->post->ID, 'beer_styles', true);
echo '<li><span class="brewing-company">'. $brewing_company .'</span> , <span class="brewing-country">'. $brewing_country .'</span>';
echo '<li><span class="beer-name">'. get_the_title() .'</span> , <span class="beer-type">'. $brewing_style .'</span></li></li>'
?>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyeleven' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
Drew Clardy comments:
Alright, I have figured this out on my own. Here is the code to make this work.
<?php
/**
* @package WordPress
* @subpackage Twenty Eleven
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php $beers = new WP_Query( array( 'post_type' => 'beer', 'orderby' => 'meta_value', 'meta_key' => 'brewing_company', 'showposts' => -1 ) ); ?>
<?php if ( $beers->have_posts() ) : ?>
<ul>
<?php while ( $beers->have_posts() ) : $beers->the_post();
$brewing_company = get_post_meta($beers->post->ID, 'brewing_company', true);
$brewing_country = get_post_meta($beers->post->ID, 'brewing_country', true);
$brewing_style = get_post_meta($beers->post->ID, 'beer_styles', true);
if ( $brewing_company != $brewing_company_name) {
echo '<li><span class="brewing-company">'. $brewing_company .'</span>, <span class="brewing-country">'. $brewing_country .'</span></li>';
$brewing_company_name = $brewing_company;
}
?>
<ul>
<?php echo '<li><span class="beer-name">'. get_the_title() .'</span>, <span class="beer-type">'. $brewing_style .'</span></li>'
?>
</ul>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyeleven' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
Luis Cordova answers:
taxonomies is the way to go, going on the same path is a headache for now and future, really