I made a custom taxonomy:
function project_register_taxonomy(){
$singular = 'Project Categories';
$plural = 'Projects Categories';
$slug = str_replace( ' ', '_', strtolower( $singular ) );
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . ' found.',
'menu_name' => $plural,
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => $$slug ),
);
register_taxonomy ('Project Categories','projects', $args);
}
add_action( 'init', 'project_register_taxonomy');
And registered it to a custom post:
function create_galblock() {
register_post_type( 'projects',
// CPT Options
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Project' )
),
'taxonomies' => array('category'),
'description' => 'Projects by Almog',
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'projects'),
'supports' => array( 'title','editor', 'thumbnail')
)
);
}
add_action( 'init', 'create_galblock' );
I need to get the post title, post thumbnail and content of custom post ‘projects’ of the custom taxonomy ‘Project Categories’.
This is my code for trying to achieve at least the content:
$args = array(
'post_type' => 'projects',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'Projects Categories',
'field' => 'id',
'terms' => '8'
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
//content
endwhile;
?>
Obviously it doesn’t return the custom posts from the custom taxonomy.
Ok, so I got this to work:
And I created a test page using
page-projects.php
template that looks like thisNow I added a test project category, which had
tag_id
61 (when you hover over it you can see it), and a test post in that category.And it showed that post just fine. now if you leave out the
fields
andterms
in yourtax_query
, this won’t work. But I’d just leave that out in the first place and get all the projects out (and figure out the sorting later).Hope this helps.
This is from the documentation:
You specified Project Categories which won’t be good as you can see above.
Update:
You also have to modify your query:
Notice the change in the
tax_query
:taxonomy
should beprojects_categories
field
should beterm_id