Rewrite rules and query for virtual page

You can create a posts archive by just setting post_type=post in your rewrite rule: function custom_archive_rule() { add_rewrite_rule( ‘custom-archive/?’, ‘index.php?post_type=post’, ‘top’ ); } add_action( ‘init’, ‘custom_archive_rule’ ); WordPress will identify this as is_home, so you’ll have to target it in pre_get_posts by the existence of your extra query variables. function custom_arcive_tax( $query ) { if … Read more

WordPress rewrite rules for state and city

Wasn’t as hard as I thought. Here’s the working example, in case anyone finds it helpful: function em_query_vars($vars) { array_push($vars, ‘state’); array_push($vars, ‘city’); return $vars; } add_filter(‘query_vars’,’em_query_vars’); function em_rewrite() { add_rewrite_rule( ‘^state/([^/]*)/([^/]*)/?’, ‘index.php?state=$matches[1]&city=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘^state/([^/]*)/?’, ‘index.php?state=$matches[1]’, ‘top’ ); add_rewrite_tag(‘%state%’,'([^&]+)’); add_rewrite_tag(‘%city%’,'([^&]+)’); } add_action(‘init’, ’em_rewrite’); function em_templates($template) { global $wp_query; if (isset($wp_query->query_vars[‘state’]) && isset($wp_query->query_vars[‘city’])) { … Read more

Change permalink for a single post entry

Try adding the following to your functions.php: function wpse221475_custom_rewrite_rules() { add_rewrite_rule( ‘^(special-project)?’, ‘index.php?post_type=projects&name=$matches[1]’, ‘top’ ); } add_action(‘init’, ‘wpse221475_custom_rewrite_rules’); Make sure to flush your rewrite rules after adding this rule. Visit: Dashboard -> Settings -> Permalinks Or if you wish, you can use flush_rewrite_rules() programmatically. To redirect requests for http://example.com/projects/special-project to http://example.com/special-project you can parse the … Read more

Change Query String to pretty permalink

Try this rewrite rule in your functions.php function string_url_rewrite() { global $wp_rewrite; add_rewrite_tag(‘%pet-page%’, ‘([^&]+)’); //In the rewrite rule you need to enter page slug and page id add_rewrite_rule(‘^Enter_Page_slug/([0-9]+)/?’, ‘index.php?page_id=Enter_Page_ID&pet-page=$matches[1]’, ‘top’); $wp_rewrite->flush_rules(true); } add_action(‘init’, ‘string_url_rewrite’, 10, 0); Now you also need to change the format of URL in anchor tag. For example, your previous URL http://www.example.com/my-account/view-pet/?pet-page=2 … Read more

Date archive permalink modification

You can change the date links by directly modifying the date_structure: function wpd_change_date_structure(){ global $wp_rewrite; $wp_rewrite->date_structure=”news/%year%/%monthnum%/%day%”; } add_action( ‘init’, ‘wpd_change_date_structure’ ); Don’t forget to flush rewrite rules after the change. WordPress will use this to generate the rewrite rules for incoming requests as well as URL output when using API functions like get_year_link, so there’s … Read more

WordPress Rewrite API calls not creating new rules

Correct solution provided by Milo in comment above. Pretty permalinks were set to plain in the admin interface and this evidently causes the WP_Rewrite to silently do nothing. I’m posting the answer here so I can mark something as correct/complete so this shows as answered. Am I doing it wrong?

How to have multiple rewrite endpoints in the same URL?

I also need this to work: example.com/nice-page/my-gallery/animals/cats/my-lightbox/1234 Where I’d like to register my-lightbox as an additional endpoint or query_var, because it could be used independently from without my-gallery, such as: example.com/another-page/my-lightbox/2345 Here’s an example of doing it using add_rewrite_rule(): add_action( ‘init’, function() { // This will let you use get_query_var( ‘my-gallery’ ). add_rewrite_endpoint( ‘my-gallery’, EP_PAGES … Read more

Rewrite-Rules not working on a vhost, everything goes to index.php

I’m not sure if this helps but its good practice, include the BEGIN and END WordPress comments around your rules as well as the check for mod_rewrite: # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ – [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress Also: … Read more

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