Monolith Multimedia
Monolith Multimedia

Reputation: 79

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?

Upvotes: 1

Views: 2463

Answers (2)

Carleton University
Carleton University

Reputation: 39

I'm curious why you would use tags, and not make use of the author page template and the native WordPress author system? There are lots of ways to do this without tags. If you need multiple authors, check out the co-authors plus plugin.

http://wp.tutsplus.com/tutorials/how-to-create-a-wordpress-authors-page-template/

http://wordpress.org/extend/plugins/co-authors-plus/

Upvotes: -1

brbcoding
brbcoding

Reputation: 13586

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.

Upvotes: 3

Related Questions