Where, When, & How to Properly Flush Rewrite Rules Within the Scope of a Plugin?

The best place to flush rewrite rules is on plugin activation/deactivation. function myplugin_activate() { // register taxonomies/post types here flush_rewrite_rules(); } register_activation_hook( __FILE__, ‘myplugin_activate’ ); function myplugin_deactivate() { flush_rewrite_rules(); } register_deactivation_hook( __FILE__, ‘myplugin_deactivate’ ); See the codex article Apologies in advance, I didn’t make it all the way through your question, so this is a … Read more

Query by post title

You can use either search parameter of wp_query : Code $args = array(“post_type” => “mytype”, “s” => $title); $query = get_posts( $args ); Or you can get posts based on title throught wpdb class: global $wpdb; $myposts = $wpdb->get_results( $wpdb->prepare(“SELECT * FROM $wpdb->posts WHERE post_title LIKE ‘%s'”, ‘%’. $wpdb->esc_like( $title ) .’%’) ); Than you’ll … Read more

Get The Post Type A Taxonomy Is Attached To

If we peek into the global $wp_taxonomies variable we see the associated object types. There might be better ways to do this or even core functions, but you could try the following: function wpse_172645_get_post_types_by_taxonomy( $tax = ‘category’ ) { global $wp_taxonomies; return ( isset( $wp_taxonomies[$tax] ) ) ? $wp_taxonomies[$tax]->object_type : array(); } then for the … Read more

Remove custom post type slug from URL

That’s how you can do first part of the job – get rid o CPT slug in post link (eg. news post type). function df_custom_post_type_link( $post_link, $id = 0 ) { $post = get_post($id); if ( is_wp_error($post) || ‘news’ != $post->post_type || empty($post->post_name) ) return $post_link; return home_url(user_trailingslashit( “$post->post_name” )); } add_filter( ‘post_type_link’, ‘df_custom_post_type_link’ , … Read more

Customize Edit Post screen for Custom Post Types?

Some of these questions are answered here: Set Default Admin Screen options & Metabox Order To remove the permalink metabox: function my_remove_meta_boxes() { remove_meta_box(‘slugdiv’, ‘my-post-type’, ‘core’); } add_action( ‘admin_menu’, ‘my_remove_meta_boxes’ ); additionaly, you will have to hide #edit-slug-box with css or javascript. see: Loading External Scripts in Admin but ONLY for a Specific Post Type? … Read more

Create category only for custom post type

I would say that you need to also create a custom taxonomy if you want it to be limited to the one post type. “Categories” is already connected to posts by default. From the WordPress Codex function people_init() { // create a new taxonomy register_taxonomy( ‘people’, ‘post’, array( ‘label’ => __( ‘People’ ), ‘rewrite’ => … Read more

Use WordPress page instead of post type archive

This one’s actually pretty easy. When you declare your post type using register_post_type, you need to add a new argument for ‘has_archive’. So you’ll add in something to the effect of: ‘has_archive’ => ‘about-cool-post-types’ Then, go to your Settings > Permalinks to flush them and it should work. I tested it locally, and this seems … Read more

Create a custom archive page for a custom post type in a plugin

What you need is hooking template_include filter and selectively load your template inside plugin. As a good practice, if you plan to distribute your plugin, you should check if archive-my_plugin_lesson.php (or maybe myplugin/archive-lesson.php) exists in theme, if not use the plugin version. In this way is easy for users replace the template via theme (or … Read more