Wordpress get_the_terms for custom Taxonomy - wordpress

Within an archive page, I'm trying to show the a custom taxonomy (called location) along with each post title, category and tag. I can't get the 'get_the_terms' to work.
The post title, category and tag is working perfectly. The taxonomy doesn't work.
<div class="post-listing">
<h4><?php the_title(); ?></h4>
<p><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<p><?php $tags = get_the_tags(); foreach($tags as $tag) { echo "$tag->name"; } ?></p>
<p><?php $terms = get_the_terms( 'locations' ); foreach($terms as $term) { echo "$term->name"; } ?></p>
</div>
This is my functions.php code.
//hook into the init action and call create_locations_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_locations_nonhierarchical_taxonomy', 0 );
function create_locations_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search Locations' ),
'popular_items' => __( 'Popular Locations' ),
'all_items' => __( 'All Locations' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location Name' ),
'separate_items_with_commas' => __( 'Separate locations with commas' ),
'add_or_remove_items' => __( 'Add or remove locations' ),
'choose_from_most_used' => __( 'Choose from the most used locations' ),
'menu_name' => __( 'Locations' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('locations','post',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'location' ),
));
}
Any ideas? It's driving me mad!

There are two parameters of get_the_terms function. They are $post and $taxonomy. Unlike the other "get_the_xxxx" functions that you can skip the $post (post object or post ID) parameter inside the loop, you must add the post object or post ID to the first parameter.
I think you should write
$terms = get_the_terms( $post, 'locations' );
Instead of
$terms = get_the_terms( 'locations' );
Documentation: https://developer.wordpress.org/reference/functions/get_the_terms/

Related

In Wordpress display custom post type single page display previous and next links for custom category

I have created a custom post type that has the ability to have custom taxonomies.
On the Single of the custom post type, I would like to be able to include "previous post" and "next post" buttons which display only if there are previous and next posts.
Set up for the custom post type in functions.php:
function the_custom_post_types() {
$types = array(
// Gallery
array(
'the_type' => 'gallery', // becomes the archive url and used in archive template name (archive-{the_type}.php)
'single' => 'single',
'plural' => 'plural',
'display' => 'Gallery',
'icon' => 'dashicons-art',
'show_in_rest' => false,
'supports' => array( 'title', 'editor', 'custom-fields', 'post_tag', 'collection', 'thumbnail' ),
'taxonomies' => array( 'collection' ),
),
// ... other CPTs ...
);
foreach ($types as $type) {
$the_type = $type['the_type'];
$single = $type['single'];
$plural = $type['plural'];
$display = $type['display'];
$icon = $type['icon'];
$gutenburg = $type['show_in_rest'];
$supports = $type['supports'];
$taxonomies = $type['taxonomies'];
$labels = array(
'name' => _x($display, 'post type general name'),
'singular_name' => _x($single, 'post type singular name'),
'add_new' => _x('Add New', $single),
'add_new_item' => __('Add New '. $single),
'edit_item' => __('Edit '.$single),
'new_item' => __('New '.$single),
'view_item' => __('View all '.$single),
'search_items' => __('Search all'.$plural),
'not_found'. => __('No '.$plural.' found'),
'not_found_in_trash' => __('No '.$plural.' found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'show_in_rest' => $gutenburg,
'menu_position' => 21,
'supports' => $supports,
'menu_icon' => $icon,
'taxonomies' => $taxonomies
);
register_post_type($the_type, $args);
flush_rewrite_rules();
}
}
add_action('init', 'the_custom_post_types');
And the taxonomy is created in a similar fashion like so:
function scaffolding_custom_taxonomies() {
$types = array(
array(
'the_type' => 'collection',
'single' => 'Collection',
'plural' => 'Collections',
'display' => 'Collections',
'post_types' => array( 'gallery' )
)
);
foreach ($types as $type) {
$the_type = $type['the_type'];
$single = $type['single'];
$plural = $type['plural'];
$display = $type['display'];
$post_types = $type['post_types'];
$labels = array(
'name' => _x( $display, 'taxonomy general name' ),
'singular_name' => _x( $single, 'taxonomy singular name' ),
'search_items' => __( 'Search ' . $plural ),
'all_items' => __( 'All ' . $plural ),
'parent_item' => __( 'Parent ' . $single ),
'parent_item_colon' => __( 'Parent ' . $single . ':' ),
'edit_item' => __( 'Edit ' . $single ),
'update_item' => __( 'Update ' . $single ),
'add_new_item' => __( 'Add New ' . $single ),
'new_item_name' => __( 'New ' . $single . ' Name' ),
'menu_name' => __( $plural ),
);
$rewrite = array(
'slug' => $the_type
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => $rewrite
);
register_taxonomy( $the_type, $post_types, $args);
flush_rewrite_rules();
}
}
add_action( 'init', 'scaffolding_custom_taxonomies', 0 );
Currently, I have used the code below for the single page.
<nav class="artwork__pagination">
<div class="artwork__prev"><?php previous_post_link( '%link', 'previous' ); ?></div>
<div class="artwork__next"><?php next_post_link( '%link', 'next' ); ?></div>
</nav>
This cycles through all the posts in the CPT rather than just those in the current category.
If I include 'true' to the array like so:
<nav class="artwork__pagination">
<div class="artwork__prev"><?php previous_post_link( '%link', 'previous', true ); ?></div>
<div class="artwork__next"><?php next_post_link( '%link', 'next', true ); ?></div>
</nav>
It returns nothing.
How can I get the links to display and limit them to only those within the same custom category?
The next and prev post link function includes a param for taxonomy, where the default is category. Since your taxonomy is named collection this should work.
/*
* The parameters for next/previous post as defined in the function.
* #param string $format Optional. Link anchor format. Default '« %link'.
* #param string $link Optional. Link permalink format. Default '%title'.
* #param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
* #param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
* #param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
*/
?>
<nav class="artwork__pagination">
<div class="artwork__prev"><?php previous_post_link( '%link', 'previous', true, '', 'collection' ); ?></div>
<div class="artwork__next"><?php next_post_link( '%link', 'next', true, '', 'collection' ); ?></div>
</nav>

Custom Post Type Taxonomy remove category slug

I have created a custom post type 'Shop' with a custom taxonomy for Shop Categories
For my shop category archive pages I want the URL structure to be:
example.com/shop/tech/ (instead of example.com/shop/category/tech/)
And I want Shop posts to be example.com/shop/shop-post-title-example
I have tried the code below and saved permalinks but when I visit a Shop post it shows a 404 error.
Is it possible to remove the category base without errors?
// Custom Post Type : Shop
function my_custom_post_shop() {
$labels = array(
'name' => _x( 'Shop', 'post type general name' ),
'singular_name' => _x( 'Product', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Product' ),
'add_new_item' => __( 'Add New' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New Product' ),
'all_items' => __( 'All Products' ),
'view_item' => __( 'View Product' ),
'search_items' => __( 'Search Products' ),
'not_found' => __( 'No projects found' ),
'not_found_in_trash' => __( 'No projects found in the Trash' ),
'menu_name' => 'Shop'
);
$args = array(
'label' => __( 'Shop' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'comments', 'revisions', 'custom-fields', 'thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'shop' ),
'has_archive' => 'shop',
);
register_post_type( 'shop', $args );
}
add_action( 'init', 'my_custom_post_shop' );
// Shop Categories
function my_taxonomies_shop() {
$labels = array(
'name' => _x( 'Shop Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Shop Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category' ),
'menu_name' => __( 'Shop Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_admin_column' => true,
'rewrite' => array( 'slug' => 'shop'),
);
register_taxonomy( 'shop_category', 'shop', $args );
}
add_action( 'init', 'my_taxonomies_shop', 0 );
You can remove the category base in custom taxonomy URLs with:
$args = array(
'rewrite' => array( 'slug' => '/', 'with_front' => false),
/* ...(other args)... */
);
register_taxonomy( 'shop_category', 'shop', $args );
And re-save Permalinks settings.
However, this will make your (basic post type) posts go 404, if you have permalinks set to Post name. It seems you cannot have both custom taxonomy and posts reside in the root. Therefore you would go to Permalinks settings and set Custom structure , e.g. /blog/%postname%/.
A side effect is that your CPTs would have this "front", too, e.g. blog/yourcustomtype. You can get rid of this with 'with_front' => false in your product type registration:
register_post_type( 'yourcustomtype', array(
'rewrite' => array(
'slug' => 'yourcustomurl',
'with_front' => false
),
/* ... */
));
Found here.
As far as I know, in order to display custom post type and/or its category page, you would need to create template files (corresponding PHP files) first. Template files would pull your posts and display them.
For example, to display your categories "shop_category" you could create a file called "taxonomy-shop_category.php".
If you like to create a template specifically for "tech" Shop Category, then you can create a file called "taxonomy-shop_category_tech.php".
There is a hierarchy of templates: https://wphierarchy.com/
Also more about templates: https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/
Nice article about custom post types taxonomies: https://code.tutsplus.com/tutorials/taxonomy-archives-list-posts-by-post-type--cms-20340
Template files should be placed in your WordPress theme folder (or child theme folder)
!important. Don't forget to update your permalinks. Go to /wp-admin/options-permalink.php and click "Save Changes".
Here is an example of a simple template file:
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
?>
<?php get_header();
$term = get_queried_object();
$args = array(
'post_type' => 'shop',
'shop-category' => $term->slug
);
$query = new WP_Query( $args );
?>
<header class="archive-header">
<h1 class="archive-title">
<?php echo $term->name; ?>
</h1>
</header>
<div id="content">
<?php
if ($query->have_posts()) {
echo'<h2>Your shop category name ' . $term->name . '</h2>';
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php endwhile;
echo '</ul>';
}
wp_reset_postdata();
?>
</div><!-- #content -->
<?php
get_footer();

Custom taxonomy loop not returning anythig

I've set up a custom taxonomy (menu_category) and custom post type (menu_item) in functions.php like this:
/* Set up custom post type for Menu Items */
function create_menu_items_post_type() {
$plugin_directory = plugins_url('images/', __FILE__ );
register_post_type( 'menu_item',
array(
'labels' => array(
'singular_name' => __( 'Menu item'),
'name' => __( 'The Menu'),
'add_new' => __( 'Add menu item'),
'add_new_item' => __( 'Add menu item'),
'edit_item' => __( 'Edit menu item'),
'new_item' => __( 'New menu item'),
'search_items' => __( 'Search menu items'),
'not_found' => __( 'No menu items found'),
'not_found_in_trash'=> __( 'No menu items found in trash'),
'all_items' => __( 'All menu items','sbr')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'menu_item'),
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'exclude_from_search' => true,
'query_var' => true,
'menu_position' => 27,
'can_export' => true,
'menu_icon' => get_template_directory_uri() . '/images/system/menu.svg',
'supports' => array('title', 'revisions', 'author'),
'capability_type' => 'post',
'taxonomies' => array('menu_category'),
'capabilities' => array(
'create_posts' => 'edit_posts',
),
'map_meta_cap' => true,
)
);
}
add_action( 'init', 'create_menu_items_post_type' );
/* Set up custom taxonomy for Menu Categories */
function create_menu_category_taxonomy() {
$labels = array(
'name' => _x( 'Menu categories', 'taxonomy general name' ),
'singular_name' => _x( 'Menu category', 'taxonomy singular name' ),
'search_items' => __( 'Search menu categories' ),
'all_items' => __( 'All menu categories' ),
'parent_item' => __( 'Parent menu categories' ),
'parent_item_colon' => __( 'Parent menu categories:' ),
'edit_item' => __( 'Edit menu category' ),
'update_item' => __( 'Update menu category' ),
'add_new_item' => __( 'Add new menu category' ),
'new_item_name' => __( 'New menu category' ),
'menu_name' => __( 'Menu categories' ),
);
register_taxonomy(
'menu_category',
'menu_item',
array(
'label' => __( 'Menu Category' ),
'rewrite' => array( 'slug' => 'menu_category' ),
'hierarchical' => true,
'labels' => $labels
)
);
}
add_action( 'init', 'create_menu_category_taxonomy' );
...and that works fine when I'm in WP admin - I can add new categories, posts etc.
But on the front-end I'm trying to return a list of all the menu_category categories and menu_item posts like this:
$the_menus = get_categories(array(
'echo' => 0,
'hide_empty' => false,
'taxonomy' => 'menu_category',
'hierarchical' => 1,
'show_count' => 0
)); ?>
<?php
foreach ($the_menus as $the_menu) {
$the_menu_args = array(
'posts_per_page' => -1,
'post_type' => 'menu_item',
'showposts' => -1,
'post_status' => 'publish',
'cat' => $the_menu->cat_ID
);
$term = get_queried_object();
$the_menu_tasks = new WP_Query($the_menu_args);
$the_menu_slug = $the_menu->slug;
$the_menu_ID = $the_menu->cat_ID;
$page_slug = $term->slug;
?>
<li>
<a href="<?php echo get_category_link( $the_menu_ID ); ?>">
<?php echo $the_menu->cat_name; ?>
</a>
</li>
<?php
}
wp_reset_postdata();
The problem is this loop returns nothing.
If I remove 'taxonomy' => 'menu_category' from $the_menus and also remove 'post_type' => 'menu_item' from $the_menu_args, it returns all the normal categories and posts (you know, regular posts and categories).
So it seems it's only failing when I specify the custom taxonomy and custom post type.
What am I doing wrong?
PS: The 'menu items' mentioned here are for a restaurant menu I'm trying to build. It has nothing to do with the WordPress menu :-P
To get the posts from custom taxonomy we need to pass tax query in arguments.
Try the below code. It works fine on my end.
<?php
$the_menus = get_terms( array(
'taxonomy' => 'menu_category',
'hide_empty' => false,
));
foreach ($the_menus as $the_menu) {
$args = array(
'post_type' => 'menu_item',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'menu_category',
'field' => 'term_id',
'terms' => $the_menu->term_id,
)
),
);
$term = get_queried_object();
$the_menu_tasks = new WP_Query($args);
$the_menu_slug = $the_menu->slug;
$the_menu_ID = $the_menu->term_id;
$page_slug = $term->slug;
?>
<!-- get category names here -->
<li>
<a href="<?php echo get_term_link( $the_menu_ID ); ?>">
<?php echo $the_menu->name; ?>
</a>
</li>
<!-- get category names here //END -->
<?php
/* GET posts according to custom taxonomy id */
if ($the_menu_tasks->have_posts()){
while($the_menu_tasks->have_posts()) : $the_menu_tasks->the_post();
echo get_the_title() .'<br />';
endwhile;
}else{
echo "Sorry! Posts not found";
}
/* GET posts according to custom taxonomy id -- END */
}
?>

WordPress Custom Taxonomy showing 404

I have created a Custom Post Type in WordPress called "Found". Inside that, I have created two taxonomies, "Pets" (named petsfound) and "Electronics" (named electronicsfound) and both of those taxonomies have various terms.
If I view the posts that use the taxonomy terms on the site, they show correctly, however, attempting to view a list of posts for the taxonomy shows a 404. So, the following happens:
https://example.com/found/pets-found/ shows a 404 page.
https://example.com/found/pets-found/dog shows the list of dogs in the pets found taxonomy.
I have tested with using both archive.php and taxonomy-petsfound.php but both show the 404 for the taxonomy. This is the same for the electronics taxonomy too.
Below is the code for the "Found" CPT and "petsfound" Taxonomy:
function found_custom_post_type() {
$labels = array(
'name' => _x( 'Found Items', 'Post Type General Name', 'lost_and_found' ),
'singular_name' => _x( 'Found', 'Post Type Singular Name', 'lost_and_found' ),
'menu_name' => __( 'Found', 'lost_and_found' ),
'name_admin_bar' => __( 'Found', 'lost_and_found' ),
'archives' => __( 'Item Archives', 'lost_and_found' ),
'parent_item_colon' => __( 'Parent Item:', 'lost_and_found' ),
'all_items' => __( 'All Items', 'lost_and_found' ),
'add_new_item' => __( 'Add New Item', 'lost_and_found' ),
'add_new' => __( 'Add New', 'lost_and_found' ),
'new_item' => __( 'New Item', 'lost_and_found' ),
'edit_item' => __( 'Edit Item', 'lost_and_found' ),
'update_item' => __( 'Update Item', 'lost_and_found' ),
'view_item' => __( 'View Item', 'lost_and_found' ),
'search_items' => __( 'Search Item', 'lost_and_found' ),
'not_found' => __( 'Not found', 'lost_and_found' ),
'not_found_in_trash' => __( 'Not found in Trash', 'lost_and_found' ),
'featured_image' => __( 'Featured Image', 'lost_and_found' ),
'set_featured_image' => __( 'Set featured image', 'lost_and_found' ),
'remove_featured_image' => __( 'Remove featured image', 'lost_and_found' ),
'use_featured_image' => __( 'Use as featured image', 'lost_and_found' ),
'insert_into_item' => __( 'Insert into item', 'lost_and_found' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'lost_and_found' ),
'items_list' => __( 'Items list', 'lost_and_found' ),
'items_list_navigation' => __( 'Items list navigation', 'lost_and_found' ),
'filter_items_list' => __( 'Filter items list', 'lost_and_found' ),
);
$args = array(
'label' => __( 'Found', 'lost_and_found' ),
'description' => __( 'Found Post Type', 'lost_and_found' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'petsfound', 'electronicsfound', 'countyfound' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'rewrite' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'found', $args );
}
add_action( 'init', 'found_custom_post_type', 5 );
}
function found_taxonomies_pets() {
$labels = array(
'name' => _x( 'Pets', 'taxonomy general name' ),
'singular_name' => _x( 'Pet', 'taxonomy singular name' ),
'search_items' => __( 'Search Pets' ),
'all_items' => __( 'All Pets' ),
'parent_item' => __( 'Parent Pets' ),
'parent_item_colon' => __( 'Parent Pet:' ),
'edit_item' => __( 'Edit Pet' ),
'update_item' => __( 'Update Pet' ),
'add_new_item' => __( 'Add New Pet' ),
'new_item_name' => __( 'New Pet' ),
'menu_name' => __( 'Pets' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'found/pets-found', 'with_front' => true ),
'show_admin_column' => true,
//'has_archive' => true
);
register_taxonomy( 'petsfound', 'found', $args );
flush_rewrite_rules();
}
add_action( 'init', 'found_taxonomies_pets', 1 );
I have reset the permalinks on multiple occasions.
I have tested amending the hierarchical and with_front values from true to false and back again in case they had any affect.
I have ran print_r($wp_query); on the 404.php template and receive the following for the start of the query_vars:
[query_vars] => Array ( [page] => 0 [found] => pets-found [post_type] => found [name] => pets-found
Not sure what else to look to, so hopefully someone can help.
Cheers
Damien
Edit - Adding the code for taxonomy-petsfound.php
<?php
/**
* The template for displaying Pets Found Taxonomy.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* #package dazzling
*/
get_header(); ?>
<div class="breadcrumb" typeof="BreadcrumbList" vocab="http://schema.org/">
<?php if(function_exists('bcn_display'))
{
bcn_display();
}?>
</div>
<?php print_r($wp_query); ?>
<section id="primary" class="content-area col-sm-12 col-md-12 <?php echo of_get_option( 'site_layout' ); ?>">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title">
Found Items 2
</h1>
<?php
// Show an optional term description.
$term_description = term_description();
if ( ! empty( $term_description ) ) :
printf( '<div class="taxonomy-description">%s</div>', $term_description );
endif;
?>
</header><!-- .page-header -->
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php dazzling_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</section><!-- #primary -->
<?php get_footer(); ?>
When it comes to the rewrite rules for post types and taxonomies, sometimes other rules match before the one you prefer. Maybe from a post or page slug. Even if it's one in the trash.
Try reviewing all your rewrite rules and seeing what is matching your URL request. You might need to change the order in which you create your post types or taxonomies. Try registering your taxonomies before your post types.
You can see the matching rewrite rules with this snippet:
function debug_rewrite_rules() {
global $wp, $template, $wp_rewrite;
echo '<pre>';
echo 'Request: ';
echo empty($wp->request) ? "None" : esc_html($wp->request) . PHP_EOL;
echo 'Matched Rewrite Rule: ';
echo empty($wp->matched_rule) ? None : esc_html($wp->matched_rule) . PHP_EOL;
echo 'Matched Rewrite Query: ';
echo empty($wp->matched_query) ? "None" : esc_html($wp->matched_query) . PHP_EOL;
echo 'Loaded Template: ';
echo basename($template);
echo '</pre>' . PHP_EOL;
echo '<pre>';
print_r($wp_rewrite->rules);
echo '</pre>';
}
add_action( 'wp_head', 'debug_rewrite_rules' );
As far as I know if you add custom taxonomies you can access the list of products or anything you want with the help of passing parameters with that like in above if you access the list you have to use the parameter like the below url :
https://example.com/found/pets-found/dog This will give you the result because here dog is the parameters and you got the dog list.
or
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
Hope this will help you

WordPress Custom Post Type Pagination Issue

Both me and my friend are completely bamboozled as to whats going on here. The quest is to have standard pagination linking from one post to the next. We can see the pages going from page/2/, page/3/ etc but the content does not change.
Here is what we've got in the custom template.
<?php
/**
* The Template for displaying all single posts.
*
* Template Name: Portfolio
*
* #package WordPress
* #subpackage Boilerplate
* #since Boilerplate 1.0
*/
get_header();
// Enable Pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array(
'portfolio'
),
'orderby' => 'date',
'posts_per_page' => 1,
'paged'=>$paged
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<article id="item<?php the_ID(); ?>" <?php post_class('post portfolio'); ?>>
<h2><?php the_title(); ?></h2>
<div>
<?php the_content(); ?>
</div>
</article>
<?php endwhile; ?>
<?php next_posts_link('« Older Entries') ?>
<?php previous_posts_link('Newer Entries »') ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
And in the bottom of the functions.php lives some custom post type script...
// Custom Post Type
function foggin_Portfolio() {
$labels = array(
'name' => _x( 'Portfolio', 'post type general name' ),
'singular_name' => _x( 'Portfolio', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Item' ),
'edit_item' => __( 'Edit item' ),
'new_item' => __( 'New Item' ),
'all_items' => __( 'All Items' ),
'view_item' => __( 'View Item' ),
'search_items' => __( 'Search items' ),
'not_found' => __( 'No item' ),
'not_found_in_trash' => __( 'No items found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Portfolio'
);
$args = array(
'labels' => $labels,
'description' => 'Holds portfolio items and portfolio specific data',
'public' => true,
'menu_position' => 5,
'rewrite' => array('slug'=>'','with_front'=>false),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'taxonomies'),
'taxonomies' => array('post_tag'),
'has_archive' => true,
);
register_post_type( 'portfolio', $args );
}
add_action( 'init', 'foggin_Portfolio' );
function portfolio_messages( $messages ) {
global $post, $post_ID;
$messages['portfolio'] = array(
0 => '',
1 => sprintf( __('Portfolio item updated. View item'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Product updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Portfolio item restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Portfolio item published. View item'), esc_url( get_permalink($post_ID) ) ),
7 => __('Portfolio item saved.'),
8 => sprintf( __('Portfolio item submitted. <a target="_blank" href="%s">Preview item</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Portfolio item scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview item</a>'), date_i18n( __( 'M j, Y # G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Portfolio item draft updated. <a target="_blank" href="%s">Preview item</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( 'post_updated_messages', 'portfolio_messages' );
function portfolio_taxonomies() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'portfolio_category', 'portfolio', $args );
}
add_action( 'init', 'portfolio_taxonomies', 0 );
?>
Thoughts, ideas, advice would be really helpful I think it's fair to say we're both stumped on this.
It may sound silly, but reset your permalinks to default, then back to your desired format. The .htaccess file needs to be rewritten after the post types have been added into functions.php
Your action for rewrite option being set to false could be causing an issue as well. If WP isn't clear on rewriting yourdomain.com/page/2/ so having the rewrite to true makes it yourdomain.com/portfolio/page/2/
Everything else seems to be in order with your query.

Resources