[resolved] Custom Query Two Word Tags (7 posts)

  1. keimcee
    Posted 5 years ago #

    I have a website where I tag some posts with the title of a Page. For example, "Raking Leaves" might be tagged with "Autumn Activities"

    Now I'm doing a query on the "Autumn Activities" page to return all posts tagged with "Autumn Activities" - this is where Raking Leaves should appear.

    My query looks like this and it's turning up empty. It works fine if the page was just one word - but when I'm querying a tag with two words it gets messed up.

    <?php $subslug = $post->post_name; ?>
    <?php $my_query2 = "tag=" . $subslug . "+anothertag&cat=34"; $my_query2 = new WP_Query($my_query2); ?>
    <?php if ($my_query2->have_posts()) : while ($my_query2->have_posts()) : $my_query2->the_post(); ?>

    The posts are definitely tagged correctly. I can't seem to figure this out!

    Any help would be greatly appreciated.


  2. Michael
    Forum Moderator
    Posted 5 years ago #

    what is the purpose of that "+anothertag within the query args?

    have you checked your variables; i.e. echoed $subslug for instance?

  3. Mike Little
    Posted 5 years ago #

    You should use the slug version of the page title ($post->post_name) which should match the slug of the tag.

  4. keimcee
    Posted 5 years ago #

    I think I am using the slug version when I use this - <?php $subslug = $post->post_name; ?>

    The another tag would be - they had to be marked with 2 tags - so one tag is one word "anothertag" or "leaves" plus a two word tag "raking leavings".

    Still haven't figured this out!


  5. Mike Little
    Posted 5 years ago #

    I've just tried a simple experiment with multi-word tags and two tag queries and it all worked fine for me.

    You need to echo your $my_query2 variable, while it is the query parameter and before it has the new WP_Query object assigned to it. (Quite frankly that's an awful way to code -- reusing that variable like that).

    Then post it here.

  6. keimcee
    Posted 5 years ago #

    It was just for test purposes the $my_query2 - but I completely agree.

    I figured out my problem! You helped and then I was still calling an old version of the variable instead of the right one in a different spot.


  7. Mike Little
    Posted 5 years ago #

    Great! Glad you've got it sorted.

Topic Closed

This topic has been closed to new replies.

About this Topic