Support

Account

Home Forums General Issues Pulling Custom Post Data into a Loop – using it throughout the loop Reply To: Pulling Custom Post Data into a Loop – using it throughout the loop

  • Hi John,

    The query is just to filter out the “product” custom post type, as the category could also be the scent too.

    `<?php get_header(); ?>

    <!– category.php–>

    <div id=”pagecontent” class=”contentwidth”>

    <h1><?php single_cat_title(); ?></h1>

    <section class=”productslist”>

    <ul class=”list-items categories”>
    <?php
    $cat = get_queried_object();
    $cat_id = $cat->cat_ID;
    $custom_query = new WP_Query(
    array(
    ‘post_type’ => array(‘post’, ‘products’),
    ‘cat’ => $cat_id
    )
    );

    if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

    <li>
    <?php // check if the post has a Post Thumbnail assigned to it.
    if ( has_post_thumbnail() ) {
    echo “<a href=”;
    the_permalink();
    echo “>”;
    the_post_thumbnail(‘thumbnail’);
    echo “</a>”;
    }
    ;?>
    <a href=”<?php the_permalink(); ?>” title=”<?php the_title();?>: $<?php the_field(‘base_price’); ?>+”>
    <p><?php the_title();?></p>
    <?php
    $post = get_field(‘container’);
    setup_postdata( $post );
    ?>
    <p class=”price”>$<?php the_field(‘base_price’); ?>+</p>
    <?php wp_reset_postdata(); // IMPORTANT – reset the $post object so the rest of the page works correctly ?>
    </a>
    </li>
    <?php endwhile; wp_reset_query();?>
    </ul>

    <?php else : ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>

    </section><!–end .categorylist–>

    </div><!–end #pagecontent.contentwidth–>
    <?php get_footer(); ?>
    `
    The whole point was to make it easier to update repeating pieces of content.

    Each product has a scent and container that is a repeating piece of content.

    There could be 50 products with one scent – having the scent as a CPT allows me to edit the scent info without editing 50 products at once. Same with container.

    Products are priced by container. So over the long term. it’s easier to change the container price over a few containers than it would be to change 200+ products pricing.

    The setup_postdata I got from somewhere in these forms and stuck with it because it worked on part of the code.