Wordpress: Get posts by tag where tag contains a name

I am working on a portfolio website with multiple artists, and each artist will have their own blog.

For each artist to have their own blog I decided for them to 'tag' themselves in posts that they make, on their portfolio pages I will list the posts with said tags.

The name of the page, is their name, so initially I thought just using:

<?php 
    $args=array('posts_per_page'=>2, 'tag' => $post->post_title);
$wp_query = new WP_Query( $args );
?>

would work, but doesn't seem to pull up any results. Yet when I echo the post title and the tag they are both displayed exactly the same.

So my next thought was to match the tag to a reg expression. Something like:

<?php
if( preg_match("/tony/i",$post->post_title)){
    echo "Tony";    
}
?>

but I do not know how to work that into a wp query.

Any idea how to do this, or if there is a better way to get to the same end result?

Answers


I quite recently was baffled by the exact same issue. I was trying to display articles based on a combination of a first and last name, but that wasn't working by just using the tag argument. I actually ended up using tag_slug__and for mine. Here's what I came up with...

<?php 

$original_query = $wp_query;
$args = array( 'tag_slug__and' => array(strtolower($first_name) . "-" . strtolower($last_name)) );
$wp_query = new WP_Query( $args );
if(have_posts()):
    echo "<h3>News Tagged " . $first_name . " " . $last_name . "</h3>"; 
    while(have_posts()) : the_post();

        $title = get_the_title();
        $content = get_the_content();
        $date = get_the_date();

            //use the vars for something suuhhhhweeeeet!



endwhile;
endif;
$wp_query = $original_query;
wp_reset_postdata();
?>

The reason I used the tag_slug__and was because the tag slug is the lowercase tag with spaces delimited by a -. This is probably not the ideal solution, and just feels hacky, but it does work.


Need Your Help

Suggestions about 2D in a “risk”-like game for iOS

objective-c ios

The idea of programming a risk-like game for iOS has been around my mind for more than a week, so I thought I should do something about it. I'm not an experienced iOS developer (I haven't done a real

Missing last item on enums

c# visual-studio-2008 enums

I was looking for some enums options, and just spotted a missing comma after last option. Take, for instance, that DayOfWeek enum (press F12 to go to definition):

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.