Custom post types and custom variables — add_rewrite_tag() not working
You’re adding the rewrite tag on admin_init, so it doesn’t exist on front end requests. Add your rewrite stuff on init instead.
You’re adding the rewrite tag on admin_init, so it doesn’t exist on front end requests. Add your rewrite stuff on init instead.
ok this i didn’t know i had to “register” variable to use function my_add_rewrite_rules() { global $wp,$wp_rewrite; $wp->add_query_var(‘book_id’); add_rewrite_rule(‘^books/([^/]*)/reviews/([0-9]+)/?$’, ‘index.php?page_id=227&bookid_id=$matches[2]’, ‘top’); // Once you get working, remove this next line $wp_rewrite->flush_rules(false); } add_action(‘init’, ‘my_add_rewrite_rules’); and in page-reviews.php i am using $book_id = $GLOBALS[‘wp’]->query_vars[‘book_id’]; One last question: how do i fix my breadcrumbs as the books/…/reviews … Read more
When you register your custom post type, you should use rewrite like this: add_action(‘init’, ‘create_location’); function create_location() { register_post_type(‘location’, array( ‘labels’ => array( ‘name’ => __(‘Locations’), ‘singular_name’ => __(‘Location’) ), ‘public’ => true, ‘rewrite’ => array( ‘slug’ => ‘location’ ) ) ); } Don’t forget to update your permalinks by going to example.com/wp-admin/options-permalink.php and click … Read more
Try adding ‘source’ as an custom query var, rather than defining it as a rewrite tag. function wpse162627_add_query_vars( $query_vars ){ $query_vars[] = “source”; return $query_vars; } add_filter( ‘query_vars’, ‘wpse162627_add_query_vars’ ); function custom_url_source() { add_rewrite_rule( ‘^source/([^/]*)$’, ‘index.php?source=$matches[1]’, ‘top’ ); } add_action(‘init’, ‘custom_url_source’);
I think I found a solution to this with a function that’s not commonly used: add_permastruct(). This does the trick of what I described above: add_rewrite_tag(‘%page%’,'([^/]+)’, ‘pagename=”); add_permastruct(“abc’,’/abc/%page%/’,false); add_rewrite_rule(‘abc/?$’,’index.php?page_id=6′,’top’); The add_rewrite_tag() defines the pagename value that I want WordPress to lookup. Then add_permastruct() defines my custom structure with that value. The last rule is just … Read more
There is a priority in URL parsing, and page related parsing is the lowest priority, or you might say that page is the default parsing applied when nothing else works. In your example wordpress will first try to treat the URL as a post and if no matching post is found it will try to … Read more
The second one works because post_type=market triggers the market post type archive. Pages have no archive, so post_type=page in the first example doesn’t point to a valid destination. Rewrite rules have to ultimately result in a successful main query. For a specific page, you could use page_id or pagename.
To answer my own question, I didn’t need to remove the ‘redirect_canonical’ filter, and I only needed to change the 2nd parameter for add_rewrite_rule. The resulting function looks as follows: function add_custom_rewrites() { add_rewrite_tag(‘%category%’, ‘(.+)’); add_rewrite_rule(‘^gallery/([^/]*)/?$’, ‘index.php?pagename=gallery&category=$matches[1]’, ‘top’); flush_rewrite_rules(); } add_action(‘init’, ‘add_custom_rewrites’);
The SSL admin and login redirection should be done through the FORCE_SSL_ADMIN constant in wp-config.php: define(‘FORCE_SSL_ADMIN’, true); For further information: Administration Over SSL Aditionally, if you want be redirected to http when you’re logged and in the frontend, take a look at: Redirect WordPress front end https URLs to http without a plugin
When you register your taxonomy, set the slug parameter of rewrite to your desired structure: register_taxonomy( ‘custom_tax’, ‘custom_post_type’, array( ‘rewrite’ => array( ‘slug’ => ‘your-cpt-slug/category’ ), // your other args… ) );