WordPress.org

Ready to get started?Download WordPress

Forums

List posts by tag with spaces (3 posts)

  1. brandonreidak
    Member
    Posted 8 months ago #

    I run an MMA website dedicated to Alaska combat sports news, etc.

    I have a page showing the title holding fighters for each fight promotion. Each title holding fighter has their own single-fighter.php page where it shows a pic + some data. Below that I want to list any posts the fighter has been tagged in.

    I've gotten success with this method, using a custom post type that the writers can use to determine what tag should be used:

    <?php
    $fighterTag = get_post_meta( get_the_ID(), 'fighterTag', true );
    query_posts("tag=$fighterTag&showposts=12"); ?>

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    .... the posts

    ^So this works as long as the tag being entered as a value for the fighterTag custom post type does not have spaces. Is there any magical way to get this puppy to work with tags that have spaces?

    Any help would be immensely appreciated!

  2. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    query_posts() expects to get the tag slug as value, not the tag title; http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

    use for instance get_term_by() to convert the tag name; http://codex.wordpress.org/Function_Reference/get_term_by

    example (untested):

    <?php
    $fighterTag = get_post_meta( get_the_ID(), 'fighterTag', true );
    $tag = get_term_by('name',$fighterTag,'post_tag');
    $tag = $tag->slug;
    query_posts("tag=$tag&showposts=12"); ?>
  3. brandonreidak
    Member
    Posted 8 months ago #

    Awe! Perfect!

    I researched so hard trying to figure that out (somewhat of a php noob here).

    That worked and I understand why!

    Many many thanks alchymyth!

Reply

You must log in to post.

About this Topic