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(
array(
'taxonomy' => 'post_tag',
'terms' => array('montly'),
'field' => 'slug',
),
);
$pages = get_pages($args);
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(
array(
'taxonomy' => 'post_tag',
'terms' => array('montly'),
'field' => 'slug',
),
);
$pages = get_pages($args);
Share
Improve this question
edited Jun 15, 2016 at 16:33
Zephy
asked Jun 15, 2016 at 16:04
ZephyZephy
111 silver badge3 bronze badges
2
- Could you show us the code you are currently using? Else it's quite difficult to say what you are doing wrong... – cjbj Commented Jun 15, 2016 at 16:17
- Added my current code to the post – Zephy Commented Jun 15, 2016 at 16:34
1 Answer
Reset to default 3Without 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 :)
EDIT
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(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'montly',
),
),
);
$query = new WP_Query( $args );