How to give a CPT (custom post type) a date based url

Assuming that your event rewrite slug is event and you want your datebased URLs to look like: http://domain.com/event/2011-06-14/ function custom_permalink_for_my_cpt( $rules ) { $custom_rules = array(); // a rewrite rule to add our custom date based urls $custom_rules[‘event/([0-9]{4}-[0-9]{2}-[0-9]{2})/?$’] = ‘index.php?post_type=event&event-date=$matches[1]’; return $custom_rules + $rules; } add_filter( ‘rewrite_rules_array’, ‘custom_permalink_for_my_cpt’ ); // add a query var so … Read more

What’s the URL for a category archive?

There is no date-based archive for a category. The /category/[slug]/ pages are already “archives”, in that they display old posts over different pages. The different pages can be accessed by adding page/2/, page/3/, … to the URL. The template tags to add these links are next_posts_link() and previous_posts_link(). If you want to add a date-based … Read more

Display posts with author in the url with custom post types

You can use the %author% tag in the rewrite property in register_post_type(). However, although the rewrite rules are added (after they’re flushed) – WordPress doesn’t replace the tag with its appropriate value when generating the permalink of your post type. For instance you end up with the permalink www.example.com/%author%/gallery-name The following replaces %author% with the … Read more

Custom slug in front of search URL

None of the above worked for me. Found another solution by overwriting the search rewrite rules using the search_rewrite_rules filter. 1 – Add this to the functions.php of your theme: add_filter( ‘search_rewrite_rules’, function($rules) { $new_rules = []; // New search slug here $new_search_slug = ‘zoeken’; foreach ($rules AS $key => $value){ $new_rules[str_replace(‘search’, $new_search_slug, $key)] = … Read more

Why is WordPress saving full-urls to the database?

If there were only relative URLs, what would they be relative to? A post can be viewed in several different contexts, including a feed, and a WordPress install can move into a different directory relative to root. There are issues with doing it either way, absolute URLs just give you a more concrete starting point … Read more

Strategy for handling hierarchical pages with empty parent content

I am using two strategies here… 1) is simple redirection to first child (using menu order) page-redirect.php <?php /* * Template Name: Redirector * Description: Empty Holder (redirect page) */ $rp = new WP_Query(array( ‘post_parent’ => get_the_id(), ‘post_type’ => ‘page’, ‘order’ => ‘asc’, ‘orderby’ => ‘menu_order’ )); if ($rp->have_posts()) while ( $rp->have_posts() ) { $rp->the_post(); … Read more

How to add multiple custom URL variables?

I pretty sure this filter lets you add an array of variables. I’ve not tested this: function add_custom_query_vars( $vars ){ $vars[] = “variable1”; $vars[] = “variable2”; $vars[] = “variable3”; //… etc return $vars; } add_filter( ‘query_vars’, ‘add_custom_query_vars’ ); Or another way of doing it would be to do this: function add_custom_query_vars( $vars ){ array_push($vars, “variable1”, … Read more