Support » Fixing WordPress » Display meta values and posts with that value

  • Resolved devjah

    (@devjah)


    I want to display meta values and posts with that value

    I’ll try to explain what I want to get:

    [meta value 1]
    [post with value 1]
    [post with value 1]
    until there are no posts with value 1
    then
    [meta value 2]
    [post with value 2]
    [post with value 2]

    until there are no more values

    and so on, until there are no more values

Viewing 6 replies - 1 through 6 (of 6 total)
  • I think this will do what you want:

    <?php // List posts by a Custom Field's values
    $meta_key = 'mykey';  // 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 = 'post'
          AND p.post_status = 'publish'
          AND m.meta_key = '$meta_key'
       ORDER BY m.meta_value, p.post_date DESC
    ";
    $rows = $wpdb->get_results($sql);
    if ($rows) {
       foreach ($rows as $post) {
          setup_postdata($post);
          if ($post->meta_value != $current_value) {
             echo "<h3>$post->meta_value</h3>";
             $current_value = $post->meta_value;
          }
          // Put code here to display the post
          the_title();
       }
    }
    ?>

    This is exactly what I need! thank you very much! I’m so happy now 🙂

    Here’s my solution:

    Instead of using a custom DB query (bad), I’m using the built in orderby query to order posts by a meta_key. Then, i get the post_meta for that key and do the magic, as outlined above:

    <?php
    		$args = array(
    			'post_type'      => 'publications',
    			'order'          => 'ASC',
    			'orderby'        => 'meta_value',
    			 'meta_key'       => 'publication_medium',
    			 'posts_per_page' => 20
    		);
    
    		query_posts( $args ); 
    
    			if ( have_posts() ) : ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    
    			<header>
    				<?php $meta_value = get_post_meta( $post->ID, '_uepi_publication_medium', true );
    					if ( $meta_value != $current_value ) {
           			  echo "<h1 class='post-title'>" . ucfirst( substr_replace( $meta_value, $meta_value . 's', 0 ) ) . "</h1>";
    			        $current_value = $meta_value;
    			      }
    				?>
    			</header>
    			<article>
    			  <?php the_content(); ?>
    			</article>
    <?php endwhile; endif; ?>

    Not sure why you think a custom query is bad. The advantage is that you do one query and then loop through the results.

    Using the standard query, you do the one before the loop and then another (get_post_meta) for each post. Adds an extra query for each post.

    I guess they’re not always bad, or inherently bad, but I bet if did a bit of benchmarking, the built in queries would be faster.

    Seems that the WP core team frowns upon custom db queries more and more each day.

    I would certainly like to know what evidence you have for that because I tend to use quite a few custom queries.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display meta values and posts with that value’ is closed to new replies.