求助:基于分类别名‘peers’获取自定义文章失败排查
Hey there! Let's break down why you might be struggling to pull custom posts using the category alias 'peers'. I've listed the most common culprits and fixes below, step by step:
Double-check the category alias spelling and case
First things first: Make sure the alias for your category is exactlypeersin your site's backend. A tiny typo (likepeerorPeers) or extra space can throw everything off—most systems treat aliases as case-sensitive and strict on formatting. Head to your category management page to confirm the alias field matches perfectly.Validate your query syntax
If you're using a system like WordPress (the most common setup for custom posts + categories), your query might be using the wrong parameters. Here's what to watch for:- If you're using the default
categorytaxonomy with a custom post type, yourWP_Queryshould look like this (replaceyour_custom_post_typewith your actual post type slug):$args = array( 'post_type' => 'your_custom_post_type', 'category_name' => 'peers', // Use category_name for aliases, not 'cat' (which uses IDs) 'posts_per_page' => 10 // Adjust to your desired number ); $peers_posts = new WP_Query($args); - If your custom post uses a custom taxonomy (not the default category), you need a
tax_queryinstead:$args = array( 'post_type' => 'your_custom_post_type', 'tax_query' => array( array( 'taxonomy' => 'your_custom_taxonomy_slug', // Replace with your taxonomy's slug 'field' => 'slug', 'terms' => 'peers', ), ), ); $peers_posts = new WP_Query($args);
Mixing up default categories and custom taxonomies is a super common mistake here.
- If you're using the default
Confirm posts are actually assigned to the 'peers' category
It sounds obvious, but don't skip this: Make sure your custom posts are actually tagged with the category that has thepeersalias. Sometimes you might select a category with a similar name but a different alias, or forget to assign the category entirely. Open up one of your custom posts and double-check the category selection.Check for theme/plugin conflicts
Some themes or plugins use hooks likepre_get_poststo modify query results, which could be overriding your custom query. To test this:- Switch to a default theme (like Twenty Twenty-Four) temporarily
- Disable all plugins
- Try running your query again. If it works, re-enable plugins one by one and switch back to your theme to find the conflicting item.
Debug the query directly
If you're still stuck, print out the raw SQL your query is running to see what's going on. For WordPress, add this line after yourWP_Querydeclaration:echo $peers_posts->request;Copy that SQL into your database tool (like phpMyAdmin) and run it. If it returns no results, either your parameters are wrong or there are no posts matching the criteria. If it does return results, the issue is likely in how you're looping through or displaying the posts.
If you've gone through all these steps and still can't get it working, share your exact code snippet and details about your custom post type/taxonomy setup—that will help narrow things down even more!
内容的提问来源于stack exchange,提问作者Lizajean

