I added a custom taxonomy called 'post_tag' to the pages, and I am trying to make a query that gets only the pages that have a specific tag set.

I already tried in several different ways, even using a custom query to get them or a query with tax_query set correctly but I just can't make it work, sometimes it returns every page even if they don't have that tag and sometimes it just breaks.

if( ! function_exists('tagpages_register_taxonomy') ){
    function tagpages_register_taxonomy()
        register_taxonomy_for_object_type('post_tag', 'page');
    add_action('admin_init', 'tagpages_register_taxonomy');

and I tried getting the pages with a query like

$args = array(
    'sort_order' => 'asc',
    'sort_column' => 'post_title',
    'hierarchical' => 1,
    'exclude' => '',
    'include' => '',
    'meta_key' => '',
    'meta_value' => '',
    'authors' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'

$args['tax_query'] = array(
        'taxonomy' => 'post_tag',
        'terms' => array('montly'),
        'field' => 'slug',

$pages = get_pages($args); 
  • Could you show us the code you are currently using? Else it's quite difficult to say what you are doing wrong... – cjbj Jun 15 '16 at 16:17
  • Added my current code to the post – Zephy Jun 15 '16 at 16:34

Without seeing the code, it's a bit hard...

But if you're trying to query for a certain taxonomy when 'post_type' => 'page', make sure you

Add a registered Taxonomy to a registered Post Type. (See Codex)

In this case, you would add it to 'post_type' => 'page' since "Pages" do not support any taxonomies by default.

Again, you may have done this and the issue is something else, but that's my best guess given th information at hand :)


So in theory, this would be the way about it using WP_Query, based on your code snippet

$args = array(
    'post_type' => 'page',
    'tax_query' => array(
            'taxonomy' => 'post_tag',
            'field'    => 'slug',
            'terms'    => 'montly',
$query = new WP_Query( $args );
  • Added my latest code to the main post – Zephy Jun 15 '16 at 16:33
  • @KristoKrisztian updated my answer to see if that helps any – Christian Rios Jun 15 '16 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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