Skip to main content
17 votes

How to disable the single view for a custom post type?

Tested all the ones mentioned above and the actual solution is simpler than any redirects suggested. In order to have archive be accessible and list the items, and single post not be accessible and ...
Benn's user avatar
  • 1,043
16 votes

Check IF is a "single product page" and Check the "role" for a Redirect

Here is the result that works, I put in the condition : is_product() to check if is a single product page. And I changed in the add_action() : admin_init by wp function cm_redirect_users_by_role() { ...
Moikben's user avatar
  • 163
12 votes
Accepted

Listing a post's categories and subcategories

Update 1: Thanks to @birgire for suggesting a better way: wp_list_categories( [ 'include' => wp_list_pluck( get_the_category(), 'term_id' ) ] ); Try this in your single.php template: $...
Minh Tri's user avatar
  • 3,419
11 votes

if post id matches these id's then do this

This looks almost correct. Let's have a look at is_single(): Works for any post type, except attachments and pages... So if the given ID isn't that of a page or attachment post type then you can ...
Howdy_McGee's user avatar
  • 20.7k
11 votes

Add_action to wp_head via functions.php

The reason the code posted is not working is that $post is not referencing the global $post variable, which is the goal here. Using get_the_ID() is a good way of accessing the ID associated with the ...
Dave Romsey's user avatar
  • 17.8k
11 votes

Custom post type single page returns 404 error

Refresh your permalinks. Go to Admin->Settings->Permalinks and hit Save. It refreshes your permalinks and should help, especially if you are changing rewrite ( 'rewrite' => array( 'slug' => ...
Ales's user avatar
  • 211
7 votes
Accepted

How do I create new content pages for my Custom Post Type?

In your dashboard go to settings/permalinks. Hit save. You should be able to see your cpts now.
rudtek's user avatar
  • 6,143
6 votes

How to check if the product is in a certain category on a single-product.php in Woocommerce?

This is old but just in case people are still looking WooThemes as a simple solution: if ( is_product() && has_term( 'your_category', 'product_cat' ) ) { //do code } *Change '...
tiadotdev's user avatar
  • 118
6 votes

How to disable the single view for a custom post type?

Working from of Sven's really good answer, I rewrote his function to make it easier to add multiple post types using in_array() in the if statement and then redirecting to the archive page instead of ...
slashbob's user avatar
  • 307
6 votes

Get_the_author doesn't return author name

Though echoing worked for the OP, the issue could be related to setting up the post manually, as was in my case. Here is the code: $post = get_post(); setup_postdata($post); // setting up the post ...
Mr_Green's user avatar
  • 159
6 votes

How to disable the single view for a custom post type?

In case you want to completely disable custom post type single view on frontend but be able to display archive page things are getting a little complicated. Setting publicly_queryable to false or ...
kierzniak's user avatar
  • 2,442
6 votes

Not Able To Render Single-CustomPostType.php File

You should correct the file names on the theme folder matching with the slugs Like: single-services_cpt.php Hope you got the error :)
Jaed Mosharraf's user avatar
5 votes

Custom SQL query to get List of posts with featured image url

This is much simpler solution without any use of complex joins, SELECT wp_posts.id, wp_posts.post_title, wp_terms.name, (SELECT guid FROM wp_posts WHERE id = ...
Krishna Modi's user avatar
5 votes
Accepted

Redirect Single Post CPT (Custom Post Type) to a specific URL

Sometimes this would be achievable with add_rewrite_url but it looks like in this case it's not as you need to redirect to a different domain. In this case you need .htaccess or nginx rules to do this,...
mozboz's user avatar
  • 2,578
4 votes
Accepted

Custom template for post type not working

get_post_format and get_post_type are completely different. Post Formats can be one of the following: 'standard' (default one) 'aside' 'chat' 'gallery' 'link' 'image' 'quote' 'status' 'video' 'audio'...
bravokeyl's user avatar
  • 3,369
4 votes

Custom post type single page returns 404 error

You should set the following in your virtualhost apache: <Directory /var/www/html/wordpress> AllowOverride All </Directory> Then, refresh your permalinks: wp-admin->...
Gustavo Siqueira's user avatar
4 votes

if post id matches these id's then do this

The function get_the_ID() will work regardless of post type (if it's a page, post, or custom post). You could use that function to perform your conditional check. if ( get_the_ID() === 2578) { ...
Ian's user avatar
  • 979
4 votes
Accepted

get_terms parent for current product only

get_terms relates to getting all the terms of a taxonomy. get_the_terms grabs all the terms related to the post. The problem is that it sounds like you only want to return those terms which are ...
socki03's user avatar
  • 1,932
3 votes
Accepted

Do not show excerpt in post content

Don't use the more tag to manually define excerpts. There's a separate input field for that. If it's not visible, go to "screen options" in the upper right corner of the edit screen to enable it. ...
cjbj's user avatar
  • 14.6k
3 votes

How to disable the single view for a custom post type?

In WordPress 5.9.0 a new filter was added which allows not only disabling the single but removing any links to it from the admin screens. add_filter( 'is_post_type_viewable', function( $is_viewable, $...
Mat Lipe's user avatar
  • 319
3 votes
Accepted

Cannot retieve the_content() and the_author() - both returning empty strings

get_the_content and the_author have to be in a loop so you would need to use other functions to get the content you need global $wp_query; $post = $wp_query->post; $page_id = $post->ID; // page ...
Gareth Gillman's user avatar
3 votes
Accepted

My single.php page does not show the related data to the post which is clicked

This probably has to do with the custom query - it's preventing other queries from working properly. Setting a logo as a post seems an odd approach. Media attachments are already posts in and of ...
WebElaine's user avatar
  • 9,440
3 votes
Accepted

Custom SQL query to get List of posts with featured image url

Worked on similar problem recently. Here is the SQL query to get the post with Featured Image. global $wpdb; $perpage = 10; $page = 1; // Get the current page FROM $wp_query $counter = $perpage * ...
JItendra Rana's user avatar
3 votes
Accepted

Insert sometext after first h3 in content

Note returning $content[0]; when !is_single() you'll get non-exist errors. You can accomplish what you're after with somthing like: add_filter('the_content', function ($content){ if (!is_single()...
David Sword's user avatar
  • 3,337
3 votes
Accepted

Wordpress block editor embeds not working in theme (single.php)

That would be because they're using oEmbed, not a shortcode. You should use the function the_content() to render the content of a post, as it applies all the things required to make embeds, shortcodes,...
cameronjonesweb's user avatar
2 votes
Accepted

Multiple comment forms on a single page

You can use output buffering to achieve this effect: function get_comments_form() { ob_start(); comments_form(); $commentsform = ob_get_contents(); ob_end_clean(); return $...
majick's user avatar
  • 5,091
2 votes
Accepted

Made Custom Post Types, but single.php not working

The admin-init hook only fires on admin screens. The correct hook to use for registering post types is init. So change: add_action( 'admin_init', 'smoelenboek' ); ... to ... add_action( 'init', '...
Andy Macaulay-Brook's user avatar
2 votes
Accepted

browser showing connection was reset while accessing posts from local host

It was syntex error in my functions.php which causing the error. In the if statement it was not wp_enqueue_scripts. It was wp_enqueue_script
user110362's user avatar
2 votes

single-{post_type}.php not working

according to your code, your cpt is "event". You will either need to change the your php to single-event.php or change this line: register_post_type('event', $args); to register_post_type('events',...
rudtek's user avatar
  • 6,143
2 votes

Create sub single pages

First, you need to register the custom post type so that it has hierarchy, i.e. a post can have a parent post. After that, you need to make sure your permalink structure is set to example.com/%...
Scott's user avatar
  • 1,325

Only top scored, non community-wiki answers of a minimum length are eligible