WordPress.org

Ready to get started?Download WordPress

Forums

Using a Custom Field Value as an $args for WP_Query (4 posts)

  1. brittgamil
    Member
    Posted 3 years ago #

    I think I am close to figuring out my issue, and my problem is probably just sematics.

    On a Page, after the loop I want to Query 5 posts with their feature images from a specific category ID. The Category ID is coming from a custom field.

    The below code works except for 1 thing... It isn't narrowing the query by the Category ID. IT is only showing the 5 most recent posts.

    Thanks for any help!

    <?php if(get_post_meta($post->ID, 'Category_ID', true)
         ): ?>    
    
        <?php
        $args=array(
          'showposts' => 5,
          'cat' => echo $Category_ID,
    
    );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="child-thumb">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         </div>
          <?php
          endwhile;
        } //if ($my_query)
    
    ?>
    <?php wp_reset_query() ?>
    <?php endif; ?>
  2. chinmoy
    Member
    Posted 3 years ago #

    <?php if(get_post_meta($post->ID, 'Category_ID', true)
         ): 
    
          $Category_ID = get_post_meta($post->ID, 'Category_ID', true)
        $args=array(
          'showposts' => 5,
          'cat' => $Category_ID,
    
    );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="child-thumb">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         </div>
          <?php
          endwhile;
        } //if ($my_query)
    
    ?>
    <?php wp_reset_query() ?>
    <?php endif; ?>
  3. sputnick3k
    Member
    Posted 3 years ago #

    This is close to MY issue if I could jump in! I am using a nested loop. I want to use the parent post to define a TAG in the custom field that i can use to pull other posts in.

    <?php if (have_posts()) : ?>
                            <?php while (have_posts()) : the_post(); ?>
                                <li>
                                   <?php $my_query = new WP_Query( "tag=CUSTOM_FIELD" );
                                   if ( $my_query->have_posts() ) {
                                       while ( $my_query->have_posts() ) {
                                           $my_query->the_post();
                                           the_content();
                                       }
                                   }
                                   wp_reset_postdata(); ?>
                                </li>
                            <?php endwhile; ?>
                        <?php endif; ?>

    I was thinking if it's easier i wouldn't mind using the post title instead. example ( "tag=single_post_title" )

    Any ideas?

  4. brittgamil
    Member
    Posted 3 years ago #

    I was able to get the code to work with a change in the arguments. The reason that it wasn't working was that the $Category_ID didn't bring in any information

    This does work:

    <?php if(get_post_meta($post->ID, 'Category_ID', true)
         ): ?>    
    
        <?php
        $args=array(
          'showposts' => 5,
        'cat' => get_post_meta($post->ID, 'Category_ID', true)
    
    );
    
        $my_query = new WP_Query($args);
    
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="child-thumb">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         </div>
          <?php
          endwhile;
        } //if ($my_query)
    
    ?>
    <?php wp_reset_query() ?>
    <?php endif; ?>

    I changed $Category_ID to calling the actual content from my custom field by using get_post_meta($post->ID, 'Category_ID', true)

Topic Closed

This topic has been closed to new replies.

About this Topic