The most reliable way to get the current post being viewed is not the global $post
variable. Instead you should first check is_singular()
, and then use get_queried_object()
to get the post object, or get_queried_object_id()
to just get the ID.
if ( ! is_singular() ) {
return;
}
$post_id = get_queried_object_id();
$args = array(
'post_type' => 'resources',
'category__in' => wp_get_post_categories( $post_id ),
'posts_per_page' => 3,
'post__not_in' => array( $post_id )
);
// etc.
Related Posts:
- Adding a span when custom post type is updated
- Display children and grandchildren of a custom post type
- WP_Query search posts by custom post type and custom taxonomy
- Conditional to modify query results
- WP_Query is printing out only one post when posts_per_page is set to multiple
- WP Query to displaying date and posts for that date?
- Can’t get order_by meta_value_num to work properly
- Taxonomy Archive: Display only one post per term from separate custom taxonomy
- Very Slow Page – How to Optimize # of Queries?
- How to make sure content doesn’t display if selection is empty
- creat filter with wp_query
- Configuring a meta query with multiple post types that have the same relationship on a single page
- How can I group posts by months and years?
- Loop increase number
- Using wp_query to modify the loop in index.php for a CPT
- WP_Query Custom Post Type if Category ID Equals
- Is This Code Efficient – Or is there a better way?
- Different options per post type in WP_Query
- Show message when query has no posts
- Custom post type and custom taxonomy 404 on page 2
- List latest post out of 2 custom post types
- Using Wp_Query, Json to add Highcharts series data
- foreach loop inside the loop creating duplicates in output
- Foreach loop returning more than one item when querying taxonomy
- Page that lists publications by classifying them by taxonomy
- set object terms after some some time of published post – functions.php
- Alike Shortcode using in Custom Shortcode
- Query custom post type that has a serialized relational advanced custom field value
- Sort custom post column by generated value?
- WP_Query – How To Query Only Custom Posts With No Children of Their Own?
- wp_post_delete is deleting all offices
- Tell wordpress to show a single page instead of an archive page
- Search result based on URL
- Custom Taxonomy – fields
- Custom Post By Category
- How to get the posts that my following users are liked?
- CPT post doesnt shows tags and category on backend admin page
- The most recent post of custom taxonomy
- WordPress loop add heading before first of type
- How to make a shortcode for my WP_Query Loop? [duplicate]
- Strange behavior on WP_query
- get_post_meta for Custom Post Type ( CPT )
- Loop carousel slider in wordpress
- Values inside a custom field to determine which category posts to display
- WordPress multi-query not returning as expected
- Trying to combine multiple WordPress queries
- Custom Post-type not returning the right child_of
- Pagination not working on homepage
- Query Custom Post Type by Tag
- How to manage wordpress knowledge base/wiki/posts collections
- Store custom post type with JSON content
- How to use a custom post type archive as front page?
- The Operator “NOT IN” Does Not Work In tax_query
- How to get the parent’s taxonomy?
- How do I redirect all 404 errors of a specific post type to another URL?
- How to save the checked boxes?
- New WP_query in template not working with CPT+category on some pages
- Displaying a specific post in a wp query by post id
- Append taxonomy terms as class names in markup?
- Optimised Code for Pulling Taxonomy Posts
- How can I get the first 50% of all posts (within a custom post type)?
- GravityForm: Populate Dropdown with custom post type [closed]
- How to output wordpress custom tags separated by comma?
- is there a way to retrieve posts that do not have a featured image assigned?
- Why does querying on post_tags (which has been applied to custom post types) only return posts?
- shortcode to show custom post types by category
- convert custom query to wp_query
- Loop to fetch 3 post_thumbnail instances from 3 most recent custom post types named “portfolio”
- How to use load more custom post type data normal or according to on click category tab In WP
- I need to add endpoint for wordpress categories
- Hide a widget inside a div on specific type of post
- Displaying custom field according to date
- Add additional field to custom post_type
- custom post for slider
- Create short URL with auto 301 redirect
- Get current user id
- How to properly use Categories with Custom Post Types
- Displaying Portfolio Filter by Category Order (alphabetically)
- Custom Post Type Query W/Category Dropdown
- How to get the custom post data using post id in WordPress?
- Pin posts to top of custom loop
- programmatically generated custom post type is created more than once
- Query custom post type and custom field by URL parameters
- Create posts inside CPT post
- Taxonomies are not showing in the category dropdown
- CPT Loop, include taxonomies and disclude others?
- Adding a nav menu for a custom post type
- How to use costum database table for custom post type
- Querying, storing, and using data from 2 separate custom post types
- date_query won’t accept day parameter, but will accept month and year for a custom post
- Custom Post ‘Post Per Page’ Crashing Page?
- Issue with permalinks for custom post type
- Paged within a custom content type template [duplicate]
- is_singular won’t call my functions?
- How to exclude certain portfolios from a loop
- show the most recent date of all posts to display on front end
- Comments are not working on Custom Post Type
- Display ACF object field data using Elementor Custom Query
- How to sort custom sortable column by custom table value
- Sort ACF by custom taxonomy