The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Posts 2 Posts
Clever URL's (4 posts)

  1. sdls
    Posted 3 years ago #

    hey there Scribu!

    Hoping you can help... I'm wondering if you've exlored custom URL sequence for related posts... for example.

    Post Type 1 - Fruit
    - Gala Apples
    - Oranges
    - Bananas

    Post Type 2 - Farmers Markets
    - Lower fairview
    - Waterfront
    - ect...

    Desire url sequence: /<market-name>/<fruit-name>

    So for "gala apples" associated with "lower fairview" url for the gala apples single page would look like "lower-fairview/gala-apples/"

    Any thoughts would be greatly appreciated.


  2. scribu
    Plugin Author

    Posted 3 years ago #

  3. sdls
    Posted 3 years ago #

    thanks for the ultra quick response Scribu! Can't seem to find the answer on the page you've suggested

    Are you suggesting I look at this tutorial?

    Doesn't seem to be the same requirements as what Rayken is looking for. I'm a bit of a newb to this topic, so please excuse my slowness in understanding

    Any insight would be greatly appreciated.

  4. sdls
    Posted 3 years ago #

    Rayken's example is the closest I can find to a plug and play code. The key difference is in his example the first url chunk is fixed. i.e. 'spare-parts/' whereas I'm hoping to make this flexible.

    // the rules
    function custom_p2p_rewrites() {
        global $wp_rewrite;
        // Define custom rewrite tokens
        $rewrite_tag = '%productName%';
        // Add the rewrite tokens
        $wp_rewrite->add_rewrite_tag( $rewrite_tag, '(.+?)', 'products=' );
        // Define the custom permalink structure
        $rewrite_keywords_structure = $wp_rewrite->root . "/spare-parts/$rewrite_tag/";
        // Generate the rewrite rules
        $new_rule = $wp_rewrite->generate_rewrite_rules( $rewrite_keywords_structure );
        // Add the new rewrite rule into the global rules array
        $wp_rewrite->rules = $new_rule + $wp_rewrite->rules;
        return $wp_rewrite->rules;
    // add to query vars
    function add_custom_p2p_rewrites( $public_query_vars ) {
        $public_query_vars[] = 'products';
        return $public_query_vars;
    // flush
    function flush_custom_rules() {
        global $wp_rewrite;
    add_action( 'init', 'flush_custom_rules' );
    add_action( 'generate_rewrite_rules', 'custom_p2p_rewrites' );
    add_filter( 'query_vars', 'add_custom_p2p_rewrites' );

    Thanks thanks thanks for your help

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic