Creating an optional prefix for both pages and posts
-
I have a checkbox which saves meta information about new posts and pages. What I am trying to achive is to have a prefix for both
post
andpage
post type, which is optional. If a post hasnew
meta information, the URL should look like/new/%postname%
. The same for pages.I am doing a custom rewrite with a custom query var:
function add_query_var($vars) { $vars[] = "new"; return $vars; } add_filter( 'query_vars', 'add_query_var' ); function custom_rewrite() { add_rewrite_rule('^new/?$', 'index.php?new=yes', 'top'); add_rewrite_rule('^new/([^/]+)/?$', 'index.php?name=$matches[1]&new=yes', 'top'); } add_action('wp_loaded', 'custom_rewrite');
add_rewrite_rule
hasname
andpagename
as parameter which is quite confusing. How do I make it work for both post types?I am also modifying
pre_get_posts
:add_action('pre_get_posts', function($query) { if($query->is_main_query() && !is_admin() && empty($_GET["p"]) && get_query_var('new') == 'yes') { $meta_query[] = [ 'RELATION' => 'AND', [ 'key' => 'new', 'value' => 'yes, 'compare' => '=' ] ]; $query->set('meta_query', $meta_query); } });
But it does not seem to work. Any help would be appreciated. Thanks!
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Creating an optional prefix for both pages and posts’ is closed to new replies.