You should use get_queried_object()
or get_queried_object_id()
to get the current custom post’s ID. IMO get_queried_object()
is a better choice here as you can conditionaly check for the post type also. Try this
function wpse258217_random_posts() {
$obj = get_queried_object();
if ( $obj->post_type === 'question' ) {
$postid = $obj->ID;
$my_query = new WP_Query( array('showposts' => '8', 'post_type' => 'question', 'post__not_in' => array( $postid ), 'orderby' => 'rand'));
//Execute the loop here
}
}
Related Posts:
- How to sort the admin area of a WordPress custom post type by a custom field
- Enable revisions for custom post type
- Add category base to url in custom post type/taxonomy
- Displaying Custom Post Types In “At A Glance” Meta Box
- How do I require authorization / login to view a specific set of posts / pages?
- How can I include custom post types on a tag page?
- WordPress Custom Post Types with a page as a parent?
- How to Make an admin_notices Message That Disappears Once the User Leaves That Particular Page?
- Page is defaulting to archive page and not designated template
- Unable to save custom taxonomy terms in a custom-built metabox
- Custom Post Type: Set post_title equal to a custom post type field
- I can’t set meta_key in my custom post type query
- Removing parent slug from URL on custom post type
- Exclude post_type from admin comments_list
- Remove POST_TYPE from custom post type permalink
- Redirect a page based on last word in slug
- How to publish custom post type for custom role user and not “submit for review”?
- Custom post types and permalink
- Custom post types, taxonomies, and permalinks
- How to noindex nofollow custom post type?
- WP All Import – Using Xpath to set a select value
- Displaying the first category from a custom taxonomy with get_the_terms
- How to make sure content doesn’t display if selection is empty
- Get term name and term parent into custom post type permalink
- How do I create an archive page as a including metadata?
- Add tag to a custom post type doesn’t change the total of custom post type in backend
- How to create custom post by using REST API securely?
- Index page with the same id as the slug
- Update postmeta Parent when post_status child change
- How to display error message in password protected form using ajax jQuery?
- Custom post listing page layout of custom post type
- Hierarchical permalinks for custom post type and taxonomy
- WordPress make Grandparent and great grandparent filterable in custom columns
- How to organize hierarchical structure in custom post type with taxonomy (like with categories)
- Order posts by 2 custom fields and if one custom field is zero orderby another custom field
- How do I control the output of a custom post type in the loop?
- Reworking function for counting custom post type posts count
- Archieve.php not loading for custom post type
- Custom taxonomy (categories) on custom post type return no results
- Custom taxonomy wp_query woes.
- Count custom post type based on two meta data
- Remove taxonomies using register_post_type_args
- Different options per post type in WP_Query
- Change custom taxonomy archive permalink
- Custom post type permalink uses archive page to display posts
- Post type Permalink
- Add ‘page template’ column to dashboard for CPTs
- Custom Post type loop with ACF not displaying properly
- Using Wp_Query, Json to add Highcharts series data
- Display the current post in browser as the first post in a loop (for a slideshow)
- Displaying One Custom Post Type’s Content On Single Post of Another Custom Post Type
- Is there a way to make child posts inherit parent post terms?
- Custom post types category
- Custom post types and custom variables — add_rewrite_tag() not working
- Use different post formats on different post types
- Add Class After 4th Post
- How to display custom post type only on a certain page?
- Setup page template array in a custom post type
- Display future posts in search
- custom post type template changes between preview and publish
- Custom post type post in custom menu
- Does “Custom Post Type” can have page hierarhy option?
- Custom Taxonomy and Categories
- custom taxonomy pagination 404 error
- Excluded Custom Taxonomy Term Posts Displaying in loop
- ACF Pro Accessing fields on a Custom Post Type
- add_rewrite_rule wordpress ignoring url pattern, wp rewrite not working
- How do I change the visible tables in the CPT dashboard?
- Convert attribute woocommerce terms (taxonomy terms) in posts of custom post type
- Taxonomy template file not working with a taxonomy that contains a slash
- How might I change the destination of a link?
- Is there a best practice, or typical way to do AJAX updating for data on a custom admin screen or post.php?
- I can not call the categories of custom post type
- Custom Taxonomy page redirecting to 404 page
- Filter a custom taxonomy from displaying a button all other taxonomies can show
- Can the wp_posts table have the same slug (post_name) in multiple statuses (post_status)?
- Imported Content Doesnt Show Up On Frontend
- Order by post meta value gets random results
- Prevent author role from editing others posts
- How to load more custom type posts through AJAX & LOAD MORE POSTS button
- Add category slug as class attribute in a link array
- Custom post type – two taxonomies
- Order Custom Posts by Several Fields
- How do display gallery embedded to the custom post type
- Trying to access custom post redirects to home page
- Rich Custom Field for Custom Post type not saving
- How to change the default orderby from “Date” to e.g. “Title” or my custom column in content type records list in admin?
- Custom Post Type – Rewrite slug [duplicate]
- How to show selected value in dropdowm menu
- Post content stays the same but permalink changes ?
- Custom Post Type Permalinks with %category%
- Pagination not working with custom loop
- How to make an admin create/edit post page to look like taxonomy page?
- Looping taxonomy in taxonomy?
- Archive Page Pagination not working
- Post types disappeared
- Gravity Forms Post method [closed]
- admin search of pages returns custom post types
- Trying to set up a range filter for related custom post types
- Dynamically count the number of custom post types associated to a custom taxonomy