• I’m going crazy…

    I’ve have spend all day trying to achive this.
    All tutorials and forums I read says the same. But it doesnt work for me!

    I have created a custom post type, with custom taxonomy.

    Everything is showing fine.
    But when I click on taxonomy-link, nothing is showing – Only a standard message: “It seems we can’t find what you’re looking for”.
    How do I get a page, for items that has the same taxonomy to show?
    ..
    When I read around:
    “take your archive.php, copy, rename it taxonomy-taxonomy-slug.php and upload to child theme.”

    So I have created a taxonomy called Locations. Here I have New York.
    Now I want all the post whit “New York” to show:
    So in order to get this to work, I take archive.php and rename it – taxonomy-locations-new-york.php.

    But it doesn’t work!
    Is that right? Or do I need to add a certain code?

    This is the code for the custom post and taxonomy:

    <?php
    /************* The Post ***********************/
    function register_job_post_type(){
    
    	$singular = 'Job';
    	$plural = 'Jobs';
    
    	$labels = array (
    		'name' 					=> $singular,
    		'singular_name' 		=> $singular,
    		'add_name' 				=> 'Add New',
    		'add_new_item' 			=> 'Add New ' . $singular,
    		'edit' 					=> 'Edit',
    		'edit_item' 			=> 'Edit ' . $singular,
    		'new_item' 				=> 'New' . $singular,
    		'view_item' 			=> 'View ' . $singular,
    		'search_term' 			=> 'Search ' . $plural,
    		'parent' 				=> 'Parent ' . $singular,
    		'not_found' 			=> 'No ' . $plural . ' found',
    		'not_found_in_trash' 	=> 'No ' . $plural . ' in Trash'
    	);
    
    	$args = array(
    		'labels' 				=> $labels,
    		'public' 				=> true,
    		'publicly_queryable' 	=> true,
    		'exclude_from_search' 	=> true,
    		'show_in_nav_menus' 	=> true,
    		'show_ui' 				=> true,
    		'show_in_menu' 			=> true,
    		'show_in_admin_bar' 	=> true,
    		'menu_position' 		=> 6,
    		'menu_icon' 			=> 'dashicons-admin-site',
    		'can_export'			=> true,
    		'delete_with_user'		=> false,
    		'hierarchical' 			=> true,
    		'has_archive' 			=> true,
    		'query_var' 			=> true,
    		'capability_type' 		=> 'post',
    		'map_meta_cap' 			=> true,
    		// 'capabilities => array(),'
    		'rewrite'				=> array(
    			'slug' 			=> 'cpt',
    			'with_front' 	=> true,
    			'pages' 		=> true,
    			'feeds' 		=> true,
    
    		),
    		'supports'			=> array(
    			'title',
    			'editor',
    			'author',
    			'custom-fields',
    			'thumbnail'
    		)
    	);
    
    	register_post_type( 'cpt', $args);
    }
    add_action( 'init', 'register_job_post_type' );
    
    /****************** Taxonomy ***********************************/
    function register_job_taxonomy(){
    
    	$plural = 'locations';
    	$singular = 'location';
    
    	$labels = array(
    		'name' 							=> $singular,
    		'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 ' . $singular . ' with commas',
    		'add_or_remove_items'			=> 'Add or remove ' . $plural,
    		'choose_from_most_used' 		=> 'Choose from most used ' . $plural,
    		'not_found' 					=> 'No ' . $plural . ' found.',
    
    	);
    
    	$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' => 'location' ),
    
    	);
    
    	register_taxonomy( 'locations', 'cpt', $args);
    
    }
    
    add_action ( 'init', 'register_job_taxonomy' );
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    What you have appears correct, there’s just one more missing element. When one queries for a taxonomy term the query seems to often default to only ‘post’ post types even though you’ve related it to ‘cpt’ and not ‘post’.

    At least this is my recollection from when I’ve been here before. I may be mis-remembering the circumstances because this just seems so fundamentally wrong that it cannot be so!

    The solution, if this is indeed the problem is to use ‘pre_get_posts’ action to check for queries involving locations taxonomy and setting the ‘post_type’ query var to ‘cpt’.

    You can quickly verify this was the problem by dumping the current ‘post_type’ value to error_log (var_dump doesn’t work here unless the output is buffered) or something. I expect it to be ‘post’. If it’s already ‘cpt’ then I’m all wet and we need to look elsewhere.

    Let me know how it goes.

Viewing 1 replies (of 1 total)
  • The topic ‘Show Taxonomy Archive Page’ is closed to new replies.