bholtbholt
bholtbholt

Reputation: 13333

Show which pages are using a tag in Wordpress

I want to link all the pages that are using a certain tag on another page. Kind of like a link back effect. For instance: page A tags itself with X so page X shows that A has tagged it.

I'm thinking it'd be the opposite of get_the_tags(), but I'm not having luck with has_tags() at the moment.

Any ideas?

This is what I'm working on now:

$lawyersPage = get_page_by_title('Lawyers')->ID;
$lawyers = get_pages( array(
    'child_of'=>$lawyersPage));

foreach($lawyers as $tag){
    if (has_tag('business-law', $tag->ID) {
    echo '
        <a href="'.get_site_url().'/lawyers/'.$tag->post_name.'">
        <li>'.$tag->post_title.'
        <span class="glyphicon glyphicon-chevron-right pull-right action"></span>
        </li></a>';

The tag is 'business-law' and I'm trying to see if the lawyers in $lawyers have used the tags.

Upvotes: 0

Views: 31

Answers (1)

J&#252;rgen Paul
J&#252;rgen Paul

Reputation: 14997

You need to do a database query:

$query=array( 'tag' => 'business-law', 'posts_per_page' => 5 );
$wp_query = new WP_Query( $query );
if ( $wp_query->have_posts() ) :
    while ($wp_query->have_posts()) : $wp_query->the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
endif;

Upvotes: 1

Related Questions