Support » Plugin: Posts 2 Posts » [Plugin: Posts 2 Posts] Adding More Arguments to Query Posts

  • I am trying to add a url rewrite rule that finds any posts that are connected to the current post. This is my rule that I am working with, but I don’t have the post_id as a part of my url, so I can’t get that via regex.

    add_rewrite_rule("cleats/([^/]+)/news", "index.php?tag=\$matches[1]", 'top');

    What I want to happen is show only related news articles. For instance, I have a Manchester United page (custom post type of club) at /clubs/manchester-united. I want /clubs/manchester-united/news to show a listing of blog articles that are connected to that post. Keeping it this way instead of via tags keeps me from having to doubly maintain a list of tags that exactly match the custom post types that I have.

    Where the query string for returning tags is as simple as ?tag=manchester-united, it works a little differently in my case. What I need is ?connected=45, where 45 is the post->ID of manchester-united. The regex in the rewrite rule above returns the slug “manchester-united”, but that isn’t enough information for my query.

    I would propose being able to pass in some other arguments into the query string. For example, instead of just an ID, maybe you pass in a post_type and a slug variable. To help be even more specific, you could also include a parent ID or parent slug. That would make a regex rewrite rule possible and scalable.

    What do you think scribu?

Viewing 1 replies (of 1 total)
  • Plugin Author scribu


    You can achieve the desired result with the current query vars, as long as you can distinguish between the club page and the /news page:

    global $wp;
    add_rewrite_rule('clubs/([^/]+)/news', 'index.php?club=$matches[1]&news=1', 'top');

    Going to:


    is equivalent to going to:


    Now, in your theme (single-club.php), you can look for the ‘news’ arg:

    if ( get_query_var('news') ) {
    query_posts( array(
      'connected' => $wp_query->get_queried_object_id()
    ) );

    where $wp_query->get_queried_object_id() returns precisely the ID of the Manchester United club.

Viewing 1 replies (of 1 total)
  • The topic ‘[Plugin: Posts 2 Posts] Adding More Arguments to Query Posts’ is closed to new replies.