0

I am using the Pages with category and tag plugin to assign tags and categories to pages. And now I would like to retrieve a list of items with specific tags associated with a page and can’t seem to get it to work.

The code I am using is as follows; kludged together from what I can find online for fetching posts based on tags like this StackOverflow post. And please excuse the very much “development” roughness in place in this stuff for now:

<?php

$tags = get_the_tags();
print_r($tags);

$args = array(
    "numberposts" => 3,
    "post_type" => "page",
    "tax_query" => array(
        array(
            "taxonomy" => "page_tag",
            "field"    => "term_id",
            "terms"    => $tags
        )
    )
);
$pages = get_pages( $args );

print_r($pages);

?>
1

2 Answers 2

4

get_pages() is a valid function, but it doesn't have any parameters for getting pages with specific taxonomy terms assigned.

WP_Query will get posts of any type (such as Pages) and can query by taxonomy terms. If you don't want to look through your plugin's code to find out what it calls the Page Tag taxonomy, in wp-admin, go to Pages > Tags and look at the URL. You will have something like /wp-admin/edit-tags.php?taxonomy=thetaxonomyname - thetaxonomyname is what you're looking for. It will be post_tag if it's regular Core tags but could be something different depending on the plugin. Once you know for sure what your taxonomy is called, you can plug that into the tax_query portion of WP_Query.

From OP's comment, the final code is

<?php
$pages = new WP_Query(
    array(
        'showposts' => -1,
        'tag' => 'thetaxonomyname',
        'meta_key' => 'date',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'post_type' => array( 'post', 'page' )
        
    )
);
print_r($pages);
?>
2
  • Thanks for the tip! I just tried the following and it works well: new WP_Query( array( 'showposts' => -1, 'tag' => 'thetaxonomyname', 'meta_key' => 'date', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_type' => array( 'post', 'page' ) ) );new WP_Query( array( 'showposts' => -1, 'tag' => 'thetaxonomyname', 'meta_key' => 'date', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_type' => array( 'post', 'page' ) ) ); Commented Jan 9, 2023 at 19:52
  • Well, it looks like I figured out another way to handle this. That WP_Query example I posted didn’t work as expected; answer coming shortly. Commented Jan 10, 2023 at 1:17
1

Okay, while WP_Query suggestion posted in an earlier answer should work for me, it wasn’t working as expected.

So I figured out how to do this using get_posts instead: Instinctively one would think to get pages one needs to use get_pages but (and this is the key) you can get pages via get_posts as well as long as post_type is set page in the arguments. I came to this conclusion when I realized that posts and pages are both stored in wp_posts so post_type should work and it indeed did work!

This is what finally worked for me; just change the $tag value to match the tag value you want to use:

<?php

$tags = array('TheTagName');

$args = array(
   'posts_per_page' => 10,
   'tag' => $tags,
   'post_type' => 'page',
   'post_status' => 'publish',
   'post_type' => array( 'page' )
);

$query = get_posts( $args );

echo '<ul class="page-list subpages-page-list">';
foreach ( $query as $post ) : setup_postdata( $post );
   echo sprintf('<li class="page_item page-item-%d menu-item">', $post->ID);
   echo "<a href='" .$post->guid . "'>" . $post->post_title . "</a>";
   echo '</li>';
endforeach;
echo '</ul>';

wp_reset_postdata();

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.