Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Values inside a custom field to determine which category posts to display

The problem is that you are passing a comma separated list of terms, not an array of term. For example, if you enter ‘army,navy,airforce’ in the custom field:

$related = array ( get_post_meta($post->ID, 'related_categories', true) );
//$related = array('army,navy,airforce');
//What you need is
//$related = array('army','navy,'airforce');

You need to transform each term into a array member, for example using explode:

$related = explode(',', trim( get_post_meta($post->ID, 'related_categories', true) ) );

Anyway, I think that you should think in another way to get your purpose. Using a custom field to manual store a list a related categories is not appropiate from my point of view. You could created, for example, a hierarchical taxonomy and set related categories under the same parent, or use the core tag taxonomy to set relationships, etc, I think this is better and more “natural” than using a custom field to store a set of taxonomy terms.

Related Posts:

  1. WP_Query search posts by custom post type and custom taxonomy
  2. Taxonomy Archive: Display only one post per term from separate custom taxonomy
  3. Different options per post type in WP_Query
  4. Custom post type and custom taxonomy 404 on page 2
  5. Foreach loop returning more than one item when querying taxonomy
  6. set object terms after some some time of published post – functions.php
  7. Custom Taxonomy – fields
  8. The most recent post of custom taxonomy
  9. get_post_meta for Custom Post Type ( CPT )
  10. WordPress custom post type
  11. Paginated Taxonomy Term Archive including one post per term
  12. Custom post query by taxonomy
  13. Querying CPT with Two Taxonomies
  14. Separate Posts and Custom Post Type in Custom Taxonomy archive template
  15. List terms from Custom Taxonomy
  16. Custom Query to display posts with custom field
  17. Get taxonomy description based on variable
  18. Displaying Metabox value (custom post type taxonomy)
  19. Optimised Code for Pulling Taxonomy Posts
  20. Show Post Count of a Category
  21. Order WordPress Custom Taxonomy Pages & Pagination Not Working
  22. How can I display my custom metaboxes on a custom post template?
  23. Conditional to modify query results
  24. Is it possible to create a shortcode that will query a post based on taxonomies?
  25. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  26. wp_query to find posts by year and month
  27. WP_Query is printing out only one post when posts_per_page is set to multiple
  28. WP Query to displaying date and posts for that date?
  29. Count posts with specific term_meta
  30. Create a random unique 6 digit number as custom field for custom post type
  31. How to output wordpress custom tags separated by comma?
  32. Querying Term Posts in Loop
  33. Custom Post Type Taxonomies -Posts not showing in Category or Tag pages
  34. WordPress Orderby Numeric Value Not Working
  35. Custom Post Type Loop within Shortcode
  36. Fetch taxonomies by custom post type id array
  37. Weird problem happening with custom taxonmy when creating/updating posts
  38. Group custom posts by custom taxonomy names
  39. Can’t get order_by meta_value_num to work properly
  40. How can i skip same post from taxonomy term?
  41. Filter by custom taxonomy slug on a custom post type
  42. Does WordPress limit the length of slug names for Post Meta or Terms?
  43. Very Slow Page – How to Optimize # of Queries?
  44. orderby in custom WP Query does not work
  45. Pagination : How to remove /page/x/ after a ‘POST’ action on a form returning to page 1
  46. Display CPT posts based on specific taxonomy
  47. How to display selected taxonomies by their parent
  48. Custom Field Order by Last Name and First Name
  49. shortcode to show custom post types by category
  50. get_posts of Custom Post Type AND Custom Taxonomy
  51. Display custom post type from dynamic custom field
  52. Query/list all terms and their custom post count
  53. Received nothing after executing AJAX post function
  54. How to make sure content doesn’t display if selection is empty
  55. Filtering WP_Query Dynamically on the Front-End
  56. Why get_posts() not returning only selected category posts from Custom Post Type?
  57. Archive for custom taxonomy lists all posts instead of current taxonomy
  58. Genesis filterable portfolio isotope [closed]
  59. wp_query check if integer exists in custom field’s array
  60. Wrong request query on cpt and tax
  61. Get custom post type list for every category shortcode
  62. Get custom taxonomies from multiple posts
  63. Pagination for Custom Taxonomy Page [duplicate]
  64. Search / Filter posts on Title/Content OR Tags
  65. WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
  66. Custom-Posttype & Custom Taxonomy WP_Query
  67. Custom loop with multiple taxonomy queries
  68. Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?
  69. 404 error when navigating to a taxonomy page with Cyrillic, what’s wrong?
  70. Custom taxonomy rewrite give pagination 404
  71. How to duplicate entire custom post type
  72. Why are taxonomy terms not saving when using custom meta boxes with radio button taxonomy selectors?
  73. WP_Query inside foreach loop returning same value for all options when filtered using ajax
  74. get_post_meta not working on publishing
  75. creat filter with wp_query
  76. Custom WP Query on custom meta and sort by multiple meta keys value
  77. WordPress loop: Show only a Custom Post Type Taxononmy TERM
  78. How can I made custom taxonomies relationship?
  79. What’s the WP way to load remaining custom posts?
  80. Custom taxonomies relationship
  81. Metabox not show in categories custom post type cmb2
  82. Categorize custom post type
  83. WordPress request fiter order by related post’s post_title
  84. Custom post type editor with dynamic selects, one drop down populating a second second drop down not working
  85. How to query Posts from a custom post type which contains a custom taxonomy?
  86. Commas not displaying in implode
  87. add custom tags for custom post type in wordpress
  88. Query Posts that have Custom Taxonomy
  89. List all Custom Post Type posts excluding certain Taxnomy term
  90. Get posts by category name
  91. WP_Meta_Query object with conditionals
  92. Displaying One Custom Post Type’s Content On Single Post of Another Custom Post Type
  93. foreach loop inside the loop creating duplicates in output
  94. Function to erase every post from a taxonomy
  95. Compare Two Custom Post Types Using The Same Custom Taxonomy
  96. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  97. Using page title as a link to term archive
  98. WP_Query order custom post type with certain meta key value by post modified date
  99. Cant’ Display Custom Post Type Title Base on Tax Terms
  100. Query custom posts from custom taxonomy
Categories custom-post-types Tags custom-post-types, custom-taxonomy, php, post-meta, wp-query
template_redirect called twice
Yearly Archive Page?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress