1

I want to pull up a custom post type called Schedule whether the permalink uses the post slug OR a value stored in the post's meta values.

E.g. /schedule/jims-schedule/ and /schedule/67sGHGj54hjs/ need to pull up the same post.

A little more info so you understand why I'm doing this:

  • The reason for the funky meta value "id" and corresponding permalink is that it's the link you can use to share with whoever you want so they can pull up your schedule, so I don't want to just use slugs because they could be easy to guess.
  • using the default permalink with post slug needs to be something that only the signed-in post author can use to view the Schedule. They'll have options to modify and manage the schedule on the front-end.
1
  • doesn't really matter but the meta key is "wsb_schedule_id" – James Revillini Oct 30 '13 at 18:44
1

I found some good information here http://codex.wordpress.org/Plugin_API/Filter_Reference/request

I added a filter to the request hook like so:

add_filter( 'request', 'alter_the_query' );

The hook function goes like this:

function alter_the_query( $request ) {

    $dummy_query = new WP_Query();  // the query isn't run if we don't pass any query vars
    $dummy_query->parse_query( $request );

    // if requesting a schedule and no posts are found with existing query vars, try the meta query
    if ( $request['post_type'] == 'schedule' && !$dummy_query->have_posts() ) {
        $posts = get_posts( array(
            'post_type' => 'schedule',
            'meta_query' => array(
                array(
                    'key' => 'wsb_schedule_id',
                    'value' => $request['schedule'],
                )
            ),
        ) );

        if ( count( $posts ) == 0 ) return $request;    // no posts found still so return the request as is for normal processing

        // a post was found, so fix the request vars and return the modified $request object for further processing
        $request['schedule'] = $posts[0]->post_slug;
        $request['name'] = $posts[0]->post_slug;
    }

    return $request;

}

My hook function runs the request with a locally scoped WP_Query object and if no schedules were returned, it tries the meta query. If the meta query succeeds, it makes the necessary modifications to the $request array and then lets Wordpress continue to process. If the meta query fails, it just returns the $request unmodified for normal processing once again.

I'm not sure how this will play out with permissions on the schedule, but that's a different ball of wax.

1
  • Not really happy with my solution. While it pulls up the correct post and feeds it into the loop, it's selecting the wrong template and I'm sure that means there are other implications I'm not even aware of yet. – James Revillini Nov 8 '13 at 15:38

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.