Support » Fixing WordPress » Call posts with same tag (using title) in specific categories

  • Resolved aputt

    (@aputt)


    I am trying to call relevant posts to the current post, but only from certain categories. Specifically, I want to use the has_tag function with the in_category function. However, I want to use the title of the post as the tag that it searches, but only within the 4 categories I’ve listed below.

    Here’s what I’ve got so far (that isn’t working):

    <?php
    $title = get_the_title();
    if(has_tag($title) && in_category(array('10','12','21','22'))){
    ?>
    Content here
    <?php } else {} endwhile; endif; ?>

    Please help! Thank you

Viewing 10 replies - 1 through 10 (of 10 total)
  • I have not tested it but maybe something like:

    <?php
    $title = get_the_title();
    $args = array(
        'category__in' => array(10,12,21,22),
        'terms' => $title
    )
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    Content here
    <?php } else {} endwhile; endif; ?>

    Thread Starter aputt

    (@aputt)

    Thanks for the reply. I’ve been playing around with your direction, and it makes sense, but it’s not working for me. It’s only calling the current post’s title and not using that title to identify the tags in other posts.

    Just as a reference, I cleaned up the code a bit. Any help would be appreciated.

    <?php
    $title = get_the_title();
    $args = array(
        'category__in' => array('10,12,21,22'),
        'terms' => $title
    );
    $the_query = new WP_Query( $args ); ?>
    <?php if ( $the_query->have_posts() ) : the_post(); ?>
    Content here
    <?php endif; ?>

    So just to be clear, the title of your post is also tags that your searching for?

    Can you post an example of a post that also is a tag?

    Thread Starter aputt

    (@aputt)

    That’s exactly it. I have multiple posts that have the same tag and in the posts from one category, I want to show all the other categories (minus the one that your in presently).

    This is the post that I’ve created that currently uses your code:
    http://www.galeriamu.com/raom-loba-2/
    The posts listed under Exhibitions, Ediciones, Revista should be the other posts that have the tag of this post’s title.

    So in this case, Raom & Loba is the title? And that is also a tag that you have for other posts?

    If you look at the tag, are you sure that it’s not actually saving it as Raom-Loba?

    Thread Starter aputt

    (@aputt)

    Yes, that is the case. The page’s address converts to Raom-Loba, but the tag uses &. You can see this below the text where it lists the tags associated with the page.

    Yes, but I did a test on a site and used Raom & Loba for a tag. Even though it displayed it like that, it was showing the tag as Raom-Loba when I moused over it. Tag=roam-loba. Try and test using Raom-Loba as the term just to see if it works. If it does then you know that is your issue which is the title and the tag are not completely the same.

    Thread Starter aputt

    (@aputt)

    Thanks, I tried what you suggested, but it’s not working. I even tried other tags that were consistent in various posts that were single words, but they didn’t work either. I also tried adding a taxonomy of post__not_in for the category where the posts are, but it continues to only show the current post and not other posts with the same tag. No idea what could be happening.

    As a reference, I’m using WPML, but that shouldn’t affect this issue. In fact, usually if I put one category to call from, it recognizes the other category in the other language as well. In this case, I have it calling the category from both languages.

    Hmmm…if you are querying posts (which you are) your answer should lie in here: http://codex.wordpress.org/Class_Reference/WP_Query

    Thread Starter aputt

    (@aputt)

    Hey there,
    I came back to this issue and discovered a couple of problems with the code, finally being able to resolve this issue with your help.

    1. You were correct, tag was coming back as raom-loba, but the title as Raom & Loba. So, I had to sanitize the title.
    2. You were also correct in regards to the query format. I’ve adjusted the code in to reflect this change.
    3. You suggested using ‘terms’, but that refers to taxonomy and what I needed was ‘tag’. This is a great website for explaining the possible query references.

    In the end, the code comes back as such:

    <?php
    $title = get_the_title();
    $titlesan = sanitize_title($title);
    $args = array(
    'tag' => $titlesan,
    'posts_per_page'=>5,
    'category__not_in'=>array(19,20)
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    Content here
    
    <?php
    endwhile;
    }
    wp_reset_query();
    ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Call posts with same tag (using title) in specific categories’ is closed to new replies.