1
votes

I'm using wordpress with custom post type UI plugin and ACF plugin. Trying to build a “single” template with multiple feeds of custom post types by custom custom taxonomy. Using this code, with a few variations to figure out what am i doing wrong.

Got 2 pieces of code like this in a row

<?php if( get_field('collectiona') ): 
    $argsc = array(
      'post_type' => 'products',
      'product-collections' => get_field('collectiona'),
    );
    $prods2 = new WP_Query( $argsc );
    if( $prods2->have_posts() ) {
      while( $prods2->have_posts() ) {
        $prods2->the_post();
        ?> 
       Whatever post code
      <?php
      }
    }
    else {
      echo '';
    }
  ?>
<?php endif; ?>

collectiona is a taxonomy field. With the piece of code, shown above, it just shows all the “products” posts out there. I’ve also tried using a text field with taxonomy slug. It shows first feed perfectly fine, if i’m not using first if statement (<?php if( get_field(‘collectiona’) ): ?>), and if that statement is present- same thing happens. All the “products” are shown. However, even with first feed shown fine, 2nd feed still shows all the “products” out there. Despite what taxonomy slug says.

I’m trying to build it the way, admin could chose a dropdown taxonomy. Text field with taxonomy slug is just an example. p.s. I use term object Full template code is here jsfiddle.net/pudfbxhv . I know jsfiddle is useless for wp templates, but that's a pretty big piece of code

EDIT

Here is updated code

<?php
            $taxterms = get_field("collectiona"); ?>                

                <?php
                $args = array(
                    'post_type' => 'products',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'product-collections',
                            'field' => 'id',
                            'terms' => $taxterm->term_id
                        )
                    )
                );

                $myquery = new WP_Query( $args );
                if($myquery->have_posts()) : ?>

                    <ul>
                        <?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
                            <li> <a href="<?php the_permalink(); ?>"><img src="<?php the_field('prod_featured_image'); ?>" onmouseover="this.src='<?php the_field('prod_hover_featured_image'); ?>'" onmouseout="this.src='<?php the_field('prod_featured_image'); ?>'" /></a>
                                                             <h2><?php the_field('prod_subtitle'); ?></h2>
                                                             <p>$<?php the_field('prod_price'); ?></p>
                                                            </li>
                        <?php endwhile; ?>
                    </ul>
                <?php endif; ?>
                <?php wp_reset_query(); ?>
1
What is the value of var_dump( get_field( 'collectiona' ) ); - Pieter Goosen
bool(false) , but value is selected in post editor - archibib
Then you are not using this function on a single page, you are using it out of context, that is why you are getting no value. Or you are using query_posts somewhere on your single template which will pass wrong info - Pieter Goosen
There is no query_posts in the template. And not sure i'm following about single page. It's used in one template for a single custom post. btw, here is full php template of it jsfiddle.net/pudfbxhv - archibib
Please add your code in your question. Your fiddle is useless as it does not work on Wordpress. I am talking about your single post page, single.php or single-{$post_type}.php - Pieter Goosen

1 Answers

0
votes

Well, that may be a perversion, but it worked for me.

$termss = get_field('collectiona');
    $slll = $termss->slug;
    $args = array(
      'post_type' => 'products',
      'product-collections' => $slll,
    );
    $lineblocks = new WP_Query( $args );
    if( $lineblocks->have_posts() ) {
      while( $lineblocks->have_posts() ) {
        $lineblocks->the_post();

Also, gotta remember to put the following code after every array

<?php wp_reset_query(); ?>