Displaying a custom post from a custom taxonomy

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:

Read More
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.

Related posts

Leave a Reply

2 comments

  1. Ok, so I got this to work:

    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');
    
    
    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' );
    

    And I created a test page using page-projects.php template that looks like this

    <?php
    /**
     * The template for displaying project pages
     * Template Name: Projects Page
     */
    
    get_header(); ?>
    
    <div id="primary" class="content-area">
    <h3>Projects</h3>
        <main id="main" class="site-main" role="main">
            <?php
    
            $args = array(
                'post_type'      => 'projects',
                'post_status'    => 'publish',
                'posts_per_page' => -1,
                'tax_query'      => array(
                    array(
                        'taxonomy' => 'project_categories',
                        'field'    => 'term_taxonomy_id',
                        'terms'    => 61,
                    )
                )
            );
    
            $the_query = new WP_Query( $args );
    
            while ( $the_query->have_posts() ) :
                $the_query->the_post();
    
                echo the_title();
    
            endwhile;
            ?>
    
        </main><!-- .site-main -->
    
    </div><!-- .content-area -->
    
    <?php get_footer(); ?>
    

    Now 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.

    enter image description here

    And it showed that post just fine. now if you leave out the fields and terms in your tax_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.

  2. This is from the documentation:

    $taxonomy: (string) (required) The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long (database structure restriction).

    You specified Project Categories which won’t be good as you can see above.

    register_taxonomy ('project_categories','projects', $args);
    

    Update:

    You also have to modify your query:

    $args = array(
        'post_type' => 'projects',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'projects_categories',
                'field' => 'term_id',
                'terms' => '8'
            )
        )
    );
    

    Notice the change in the tax_query:

    taxonomy should be projects_categories

    field should be term_id