• Resolved R22

    (@r22)


    Hi,

    I installed Custom Post Type UI plugin and I created Custom Post Type called “products” which has a bult-in taxonomy Tags.

    Now what I need, I created several custom posts and some of them have a several same tags. I need to create “Related products” on my page and I want to achieve it using tags. I used google a lot, the best article which seemed to fit my needs is this

    According to article, I put this code into my single-products.php file

    `
    <?php
    if ( ‘products’ == get_post_type() ) {
    $taxs = wp_get_post_terms( $post->ID );
    if ( $taxs ) {
    $tax_ids = array();
    foreach( $taxs as $individual_tax ) $tax_ids[] = $individual_tax->term_id;

    $args = array(
    ‘tag__in’ => $tax_ids,
    ‘post__not_in’ => array( $post->ID ),
    ‘showposts’ => 5,
    ‘ignore_sticky_posts’ => 1
    );

    $my_query = new wp_query( $args );

    if( $my_query->have_posts() ) {

    echo ‘

    <ul>’;

    while ( $my_query->have_posts() ) :
    $my_query->the_post();

    echo ‘

    <li><a href=”‘ . the_permalink() . ‘”>’ . the_title() . ‘ </a></li>
    ‘;

    endwhile;

    echo ‘</ul>
    ‘;

    }

    wp_reset_query();

    }
    }
    ?>
    `

    The problem is it had no effect. It shows no related products (=custom posts). Is there any way how to fix it? Or is there any way how to display tag related custom posts?

Viewing 3 replies - 1 through 3 (of 3 total)
  • created Custom Post Type called “products” which has a bult-in taxonomy Tags.

    is that a custom taxonomy?

    you might need to reference that in:
    http://codex.wordpress.org/Function_Reference/wp_get_post_terms

    if it is a custom taxonomy (and not the default ‘post_tag’), you might need to use a different query parameter;

    http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    what is the result if you add a test output after this line:
    $taxs = wp_get_post_terms( $post->ID );
    for example with:
    var_dump( $taxs );

    Thread Starter R22

    (@r22)

    Hi alchymyth,

    no, there is no custom taxonomy, I used taxonomy Tags, which is included in WP by default. I just assigned this default Tags taxonomy to my Custom Post Type “products” so I could use it. I hope you know what I mean.

    So basically, I want to use default ‘post_tag’.

    what is the result if you add a test output after this line:
    $taxs = wp_get_post_terms( $post->ID );
    for example with:
    var_dump( $taxs );

    The output of var_dump ($taxs; is this:

    array(2) { [0]=> object(stdClass)#2337 (9) { [“term_id”]=> string(2) “13” [“name”]=> string(5) “Tag 1” [“slug”]=> string(5) “tag_1” [“term_group”]=> string(1) “0” [“term_taxonomy_id”]=> string(2) “16” [“taxonomy”]=> string(8) “post_tag” [“description”]=> string(0) “” [“parent”]=> string(1) “0” [“count”]=> string(1) “3” } [1]=> object(stdClass)#2338 (9) { [“term_id”]=> string(2) “11” [“name”]=> string(5) “Tag 2” [“slug”]=> string(5) “tag_2” [“term_group”]=> string(1) “0” [“term_taxonomy_id”]=> string(2) “14” [“taxonomy”]=> string(8) “post_tag” [“description”]=> string(0) “” [“parent”]=> string(1) “0” [“count”]=> string(1) “3” } }

    I guess that is correct, because I added 2 tags to my custom post (‘Tag 1’ and ‘Tag 2’ are names of tags assigned to this custom post)

    Thread Starter R22

    (@r22)

    So I figured it out. The problem was I didn’t have the ‘post_type’ defined in $args for new WP_query

    It works now

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Tags related custom posts’ is closed to new replies.