Support » Plugins » Hacks » Custom Query Two Word Tags

  • Resolved keimcee


    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.


Viewing 6 replies - 1 through 6 (of 6 total)
  • Michael


    Forum Moderator

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

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

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

    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!


    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.

    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.


    Great! Glad you’ve got it sorted.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Query Two Word Tags’ is closed to new replies.