0

I'm having a real headache with this one, and not sure what to try. Basically, I'm building a website with a custom post type of 'charities'. There are many charities listed, we'll use RSPB as an example.

SO visiting "example.com/charities/rspb" will load the custom post type, and this works fine. What I want to do is pass a variable to change the page content:

example.com/charities/rspb/contacts/

I am looking for a query var of 'contacts' to be true if this URL is passed, or similarly:

example.com/charities/rspb/donation-information/

... to make a query var of 'donation-information' to be set to true if this URL is passed, etc etc.

I've tried adapting this solution: Passing variables through permalink structure

And had no luck. It works on the 'charities' page (which loads the archive-charities.php template file just fine, and can pass a variable to it) but I can't get anything to work for passing variables to the custom posts themselves.

Some additional info, I'm running WP 3.7 on an Apache2 server (ubuntu). Any ideas or any pointers would be great. I've spent quite a bit of time researching this and the permalink structure, and I am still in the dark as to how to make it work for my scenario. Thanks in advance for any help.

** EDIT - Here's my code! (thanks for the heads up here!) **

    // Register the variables that will be used as parameters on the url
    function add_my_var($public_query_vars) {

        array_push($public_query_vars[], 'contacts', 'donation-info', 'comments');
        return $public_query_vars;
    }

    add_filter('query_vars', 'add_my_var');

    function do_rewrite() {

        foreach (array('contacts', 'donation-info', 'comments') as $item) {
            add_rewrite_rule('(charities)/[/]?([^/]*)$', 'index.php?pagename=charities&'.$item.'=$matches[2]','top');
        }

    }
    add_action('init', 'do_rewrite');

    // ...Get the query vars on template file
    get_query_var('contacts');
4
  • Please add the code you're currently trying to get to work.
    – kaiser
    Oct 30, 2013 at 11:12
  • Code: // Register the variables that will be used as parameters on the url function add_my_var($public_query_vars) { array_push($public_query_vars[], 'contacts', 'donation-info', 'comments'); return $public_query_vars; } add_filter('query_vars', 'add_my_var'); function do_rewrite() { foreach (array('contacts', 'donation-info', 'comments') as $item) { add_rewrite_rule('(charities)/[/]?([^/]*)$', 'index.php?pagename=charities&'.$item.'=$matches[2]','top'); } } add_action('init', 'do_rewrite'); // ...Get the query vars on template file get_query_var('contacts'); Oct 30, 2013 at 11:54
  • Hehe :) Sorry, I meant in an edit. You might want to read a bit in the help center to see How to Ask and how the site works in general.
    – kaiser
    Oct 30, 2013 at 11:56
  • haha sorry about that! done!! Oct 30, 2013 at 11:59

1 Answer 1

1

The one issue I see immediately with your rewrite rule is this part:

index.php?pagename=charities

You don't have a page named "charities", you have a custom post type, so that should be:

index.php?charities=$matches[1]

assuming your custom post type query var is charities.

HOWEVER, that said, I would not add rewrite rules for this, WordPress will take care of much of the heavy lifting if you use add_rewrite_endpoint instead:

add_rewrite_endpoint( 'contacts', EP_PERMALINK );
add_rewrite_endpoint( 'donation-info', EP_PERMALINK );
add_rewrite_endpoint( 'comments', EP_PERMALINK );

After adding these and flushing rewrites, you will be able to check if the contacts, donation-info, and comments array keys exist in $wp_query->query_vars.

1
  • That worked a treat thanks, that's a really nice implementation, thanks for the help on this. Happy Halloween! Oct 31, 2013 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.