• 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 and page post type, which is optional. If a post has new 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 has name and pagename 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!

    • This topic was modified 5 years, 4 months ago by Jan Dembowski. Reason: Fixed horribly broken formatting
Viewing 6 replies - 1 through 6 (of 6 total)
  • It’s not a good idea to be changing your URLs. Think of the links pointing to your site and also any links internal to your site. The URL structure is called permalink for a reason (it doesn’t change).
    What you are proposing would be better handled with a taxonomy instead of a meta field.

    Thread Starter Kristiyan Katsarov

    (@katsar0v)

    @joyously can the same behaviour be reproduced with taxonomies? Taxonomies are dynamic, I just need one new/ prefix.

    It won’t be the same as you are describing, because it doesn’t change the permalink. But you can access a taxonomy page (a list of posts using a taxonomy term) with a URL that describes that, instead of a different URL for each post or page with the condition.

    Taxonomies

    Thread Starter Kristiyan Katsarov

    (@katsar0v)

    I could try this. But is there really no way to add a rule via add_rewrite_rule for both posts and pages? Afterwards I just make $query->set('post_type',['post','page']) in pre_get_posts and it should work. The problem is just with add_rewrite_rule

    Moderator bcworkz

    (@bcworkz)

    Yes it should work. Did you remember to visit the permalinks settings screen so the rules are flushed? You ought to be able to use the “name” URL parameter for pages even though it’s really meant for posts.

    I second what Joy said though. The post is not going to be new forever is it? So why should it be in its permanent link? I don’t see a problem with using a “new” category, tag, or other taxonomy term. The post’s URL ends up being something like example.cοm/category/new/post-slug/. You can change the category base element to anything you like. You can also associate categories with pages even though it’s not so by default. Then it’s simple to remove the “new” term any time it’s appropriate without altering the post’s permalink and losing any SEO karma it may have accumulated.

    Thread Starter Kristiyan Katsarov

    (@katsar0v)

    Yes it should work. Did you remember to visit the permalinks settings screen so the rules are flushed? You ought to be able to use the “name” URL parameter for pages even though it’s really meant for posts.

    Unfortunately name works only for posts and pagename only for pages. Tried saving the permalinks, didn’t help much. I found another solution which does not include categories or add_rewrite_rule. Maybe it would be best to create a custom taxonomy for this, but I really don’t want the content of this taxonomy to be managed in the database (though it might be an option for later).

    I think it might be a bug, or maybe not, but add_rewrite_rule() works either for posts or for pages. Maybe I need to call it twice? No idea. Thanks for the help anyway.

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.