WordPress.org

Ready to get started?Download WordPress

Forums

Using Custom Field To Show 5 Posts of Category (9 posts)

  1. charvan
    Member
    Posted 1 year ago #

    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.

  2. charvan
    Member
    Posted 1 year ago #

    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));
                    ?>
  3. charvan
    Member
    Posted 1 year ago #

    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();
       }
    
     } ?>
  4. Ruturaaj Patki
    Member
    Posted 1 year ago #

    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.

  5. charvan
    Member
    Posted 1 year ago #

    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; ?>
  6. Ruturaaj Patki
    Member
    Posted 1 year ago #

    <? $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.

  7. charvan
    Member
    Posted 1 year ago #

    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.

  8. charvan
    Member
    Posted 1 year ago #

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

  9. charvan
    Member
    Posted 1 year ago #

    HO LEE KWRAP!!!

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

Topic Closed

This topic has been closed to new replies.

About this Topic