Support » Fixing WordPress » Using Custom Field To Show 5 Posts of Category

  • I am a PHP hack, but slowly trying to not be one.

    Scenario

    • Page template for “Artists”
    • Custom Field Name: “art_cat”
    • Custom Field Value that matches Category of same name/syntax
    • Able to call in that value via the following:
    <?php
    
         $artistcategorygetter = get_post_meta($post->ID, 'art_cat', true);
    
         if ($artistcategorygetter) ?>
         <?php echo $artistcategorygetter; ?>

    Hoping to figure out how to use WP_Query or other method to pull in recent posts from said category.

    I have been all over the forums and Google for 10+ hours now and am unable to find a solution that fits. Starting to understand PHP a bit more as a result, but this seems like a very simple thing to do and I am either missing something or the solution is far too over my head.

    Please help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Don’t understand why this does not work:

    <?php
                        global $wp_query;
    
                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
                        $art_cat = (get_post_meta($post->ID, 'art_cat', true));
    
                        query_posts(array('posts_per_page' => '10','paged'=>$paged,'category_name'=>$art_cat));
                    ?>

    This pulls in five most recent posts but does not limit by category:

    <?php
    	   $my_query = new WP_Query(array(
      'Category_name'  => get_post_meta($post->ID, 'art_cat', true),
      'posts_per_page' => 5,
    ));
    
    if ($my_query->have_posts()){
    
       while ($my_query->have_posts()){
    
         $my_query->the_post();
    
         the_title();
         the_excerpt();
       }
    
     } ?>

    From your description, it looks like you’re able to gather the Category name from Custom Fields. In that case, see the code below:

    $query = new WP_Query( array ("category_name" => $artistcategorygetter, "posts_per_page" => "5") );

    Once your $query object is set, rest of the things are same as regular WordPress loop.

    <?php if ($query->have_posts()) : ?>
    <!-- here goes your DIV container HTML. -->
    
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
    <!-- here goes your post content block. go on using the_title, the_content etc. as and where required. -->
    
    <?php endwhile; ?>
    <?php endif; ?>

    Well, I must submit one thing here… I’ve directly typed the code above in this comment area. Please be aware of syntax errors due to my typing mistakes. 🙂

    I hope this helps.

    Cheers,

    Ruturaaj.

    Ruturaaj, thank you so much for your reply. Think I am getting closer but the following does not limit by category and only works when the “C” in “Category_name” is capitalized. Weird, no?

    <?php
    $artistcategorygetter = get_post_meta($post->ID, 'art_cat', true);  if ($artistcategorygetter)
    ?>
    
    <? $query = new WP_Query( array (
      'Category_name' => $artistcategorygetter, 'posts_per_page' => '5'));
    ?>
    
    <?php if ($query->have_posts()) : ?>
    <!-- here goes your DIV container HTML. -->
    
    <?php while ( $query->have_posts() ) : $query->the_post();
    
    the_title();
    the_excerpt();
    
    ?>
    
    <!-- here goes your post content block. go on using the_title, the_content etc. as and where required. -->
    
    <?php endwhile; ?>
    <?php endif; ?>
    <? $query = new WP_Query( array (
      'Category_name' => $artistcategorygetter, 'posts_per_page' => '5'));
    ?>

    Just noticed one thing… your PHP opening tag. Although it’s valid to open PHP with <?, my Server doesn’t allow me to have such notations for PHP start tag. It forces me to have “<?php” as opening tag.

    Hahah. Thank you for being so gentle in pointing that out. When fixed, I am still in the same quandary though.

    Seems like I am getting close but more and more frustrated. This seems so simple and like something people do with WP all the time.

    Have gone through and verified that my custom field value matches the category name over and over so that is not it.

    HO LEE KWRAP!!!

    I figured it out with your help, Ruturaaj! Thank you. Will post solution here soon.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Using Custom Field To Show 5 Posts of Category’ is closed to new replies.