How to get the clean permalink in a draft?

This is a little “hacky”, but when you call get_permalink and you need the permalink for a draft, provide a clone of your post object with the details filled in: global $post; if ( in_array( $post->post_status, array( ‘draft’, ‘pending’, ‘auto-draft’ ) ) ) { $my_post = clone $post; $my_post->post_status=”publish”; $my_post->post_name = sanitize_title( $my_post->post_name ? $my_post->post_name … Read more

Removing unnecessary numbers appended to postname in permalinks

There is a plugin for wordpress which allows you to handle 404 errors and redirect to specific pages. So if wordpress calls your post /events/2013/june/fishing-trip-2, you could go into the plugin and add a rule so that /events/2013/june/fishing-trip points to /events/2013/june/fishing-trip-2. It’s not a perfect solution, as you’d have to manually enter each duplicate post … Read more

Taxonomy, Terms, and Template Files

These types of URLs are supported since WP 3.1: register_taxonomy( ‘types’, ‘post’, array( ‘hierarchical’ => true, ‘rewrite’ => array( ‘hierarchical’ => true ), … ); Remember to flush the rewrite rules after making the change. The template you would use for both parent and child terms is taxonomy-types.php: $current_term = get_queried_object(); if ( 0 == … Read more

Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/

That happens because WordPress reads your old post name as category name now – and it cannot find that category. Solution: filter 404_template and try to find the post and its permalink. Then redirect. <?php # -*- coding: utf-8 -*- /* Plugin Name: Redirect to category */ add_filter( ‘404_template’, ‘t5_redirect_to_category’ ); function t5_redirect_to_category( $template ) … Read more

Custom post type 404s with rewriting even after resetting permalinks

Step 1, add the rewrite tags for custom event year and month query vars, then register the event post type with those tags in the slug argument of the rewrite argument: function wpa83531_register_event_post_type(){ add_rewrite_tag(‘%event_year%’,'(\d+)’); add_rewrite_tag(‘%event_month%’,'(.+)’); register_post_type( ‘event’, array( ‘public’ => true, ‘rewrite’ => array( ‘slug’ => ‘events/%event_year%/%event_month%’ ), ‘has_archive’ => false, ‘hierarchical’ => false, ‘supports’ … Read more

Setting 404 page in Nginx

Remove the ‘fastcgi_intercept_errors’ argument from you configuration. It’s unnecessary since ‘error_page’ declares 404 errors, which should be handled by index.php, which will trigger PHP-FPM to handle it, and WordPress to present your theme’s 404 page. It seems counter-intuitive, but ‘fastcgi_intercept_errors’ is actually causing PHP not to handle the error page.

Add a URL prefix to permalinks of one category of posts only

Suppose articles is slug of the category. 1. Add a custom rewrite rule: add_action(‘init’, function() { add_rewrite_rule(‘^articles/([^/]+)/?$’, ‘index.php?name=$matches[1]’, ‘top’); }, 10, 0); 2. Filter the post link: add_filter(‘post_link’, function($post_link, $post, $leave_name = false, $sample = false) { if ( has_category(‘articles’, $post) ) { $post_link = str_replace(“https://wordpress.stackexchange.com/” . $post->post_name, ‘/articles/’ . $post->post_name, $post_link); } return $post_link; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)