WordPress.org

Forums

Loop inside single post based on custom field values (3 posts)

  1. mistercyril
    Member
    Posted 3 years ago #

    Hello,

    I am trying to display a list of posts at the bottom of my single.php template, that are defined by a custom field.

    The idea is to use a post's custom field to set a few post "id"s that would be used to populate the list of posts.

    I can get a loop working but its not using the post ids I defined. It seems to be ignoring them all together.

    Could anyone give me a quick example of what my query should look like?

    The custom field Key is "Products" and the values should be ids separated by commas.

    Thanks in advance,
    C.

  2. Tom Morton
    Member
    Posted 3 years ago #

    Funny you mention this, I did a similar thing not with posts but with pages and a custom theme menu. I think this might work:

    <?php 
    
        $single_posts = echo get_post_meta($post->ID, 'Products', true);
    
        $post_array = array($single_posts); //construct array for single posts id's
    
    	$args = array('order' => 'desc', 'p' => $post_array);
        $single_post_fields = new WP_Query($args); 
    
        $count = 0; ?>
        <div role="complementary" class="homepage-widgets" id="footer-widget-area">
    
    	<?php while ($single_post_fields->have_posts()) : $single_post_fields->the_post();
        $count++;
        $do_not_duplicate = $post->ID; ?>
    
    		<p>Whatever your HTML and WP stuff is to call the post (the_title, the_content, etc)</p>
    
        <?php endwhile; wp_reset_query(); ?>

    I haven't tried it, but its similar to my page code so it should work.

  3. mistercyril
    Member
    Posted 3 years ago #

    Hey, Thanks for your code.

    I tried it out but I can't get it to work entirely.

    FYI : I have a comma separated list of ids in my "Product" field (i.e. "1, 56, 98, 203").

    • If I test the $single_posts variable it lists the post ids as it should.
    • If I test the $post_array variable it outputs "array" instead of the values of the array.

    Here are my findings so far. Do you have any ideas?

    Here's the non working version

    <?php $products = get_post_meta($post->ID, 'products', true);
    			if($products != '') {
    			$single_posts = get_post_meta($post->ID, 'products', true);
    			$post_array = array($single_posts); //construct array for single posts id's
    			$args = array(
    				'order' => 'desc',
    				'p' => $post_array
    			);
    			$single_post_fields = new WP_Query($args);
    			$count = 0; ?>
                	<div class="categoryimages">
                    <?php echo $args ?>
                        <ul>
                	<?php while ($single_post_fields->have_posts()) : $single_post_fields->the_post(); $do_not_duplicate = $post->ID; ?>
                    		<li><?php the_post_thumbnail('thumbnail'); ?></li>
        			<?php endwhile; wp_reset_query(); ?>
                        </ul>
                    </div>
    
                <?php } ?>

    This version displays the first post in the list of ids (better, but I need it to display all of them)

    <?php $products = get_post_meta($post->ID, 'products', true);
    			if($products != '') {
    			$single_posts = get_post_meta($post->ID, 'products', true);
    			$post_array = array($single_posts); //construct array for single posts id's
    			$args = array(
    				'order' => 'desc',
    				'p' => $single_posts
    			);
    			$single_post_fields = new WP_Query($args);
    			$count = 0; ?>
                	<div class="categoryimages">
                    <?php echo $args ?>
                        <ul>
                	<?php while ($single_post_fields->have_posts()) : $single_post_fields->the_post(); $do_not_duplicate = $post->ID; ?>
                    		<li><?php the_post_thumbnail('thumbnail'); ?></li>
        			<?php endwhile; wp_reset_query(); ?>
                        </ul>
                    </div>
    
                <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic