0

In my custom post type, I want to allow the "admin" to pull related posts from another custom post type taxonomy. The admin should be able to put the taxonomy in a custom field and this will query posts from the taxonomy they choose.

The custom field is "featured_product_tag" - in the field a taxonomy from the "products" post type will be pulled. Why doesn't this work? Thanks for your help in advance.

 $foo = get_post_meta( $post->ID, 'featured_product_tag', true );

does echo the correct taxonomy but it doesn't pull from this taxonomy.

<?php 
$foo = get_post_meta( $post->ID, 'featured_product_tag', true );
$args = array(
    'post_type'=> 'products',
    'taxonomy' => $foo,
    'order'    => 'rand',
    'showposts' => '4'
    );              
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$url = $thumb['0'];
?>
<a href="<?php echo get_permalink( $post->ID ); ?>">
<div class="related-featured">
<img src="<?=$url?>" style="max-height: 180px;" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
<span class="small-text-p"><i>from</i> <?php  echo  get_post_meta(get_the_id(), 'price_low', 'true'); ?></small>
</div></a>
<?php endwhile; else: ?>
<?php endif; wp_reset_postdata(); ?>
<?php endif; ?>
  • Is there WooCommerce plugin installed ? – CodeMascot Oct 25 '16 at 9:26
  • No - this is just in the custom post type named "products" - with the custom taxonomy "cosmetics" attached to it. I want to query 4 random products when the admin adds the "taxonomy" in the field. – Dan Bruce Oct 25 '16 at 9:42
0

You cant run query based on taxonomy only. You also need to provide the taxonomy term the custom post type post associated with. Change your $args like below -

$args = array(
    'post_type'=> 'products',
    'tax_query' => array(
        array(
            'taxonomy' => 'cosmetics', // your taxonomy
            'field' => 'term_id',
            'terms' => get_term_by('name', $foo, 'cosmetics')->term_id, // your taxonomy term id or array of id's
        )
    ),
    'order'    => 'rand',
    'showposts' => '4'
);

For more information on WP_Query() arguments please read this.

  • Can I know why you've remove acceptance ? – CodeMascot Oct 25 '16 at 10:06
  • You've to save the taxonomy term as post meta or you need save both taxonomy and taxonomy term to post meta. Then pass the taxonomy and taxonomy term to the next query and you'll get the data. Hope I made myself clear. You can't query posts based on taxonomy. – CodeMascot Oct 25 '16 at 10:10
  • Did you see my edit? It's not pulling anything. Thanks so much for your help. – Dan Bruce Oct 25 '16 at 10:10
  • See my comment above. – CodeMascot Oct 25 '16 at 10:10
  • I have a taxonomy "tax1" with 4 posts in the tax. I go back to the post and add "tax1" in the field "featured_product_tag", from this, it should query 4 posts from "tax1" category. The taxonomy term is already in the post meta. – Dan Bruce Oct 25 '16 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.