WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
Adding More Arguments to Query Posts (2 posts)

  1. Derek Perkins
    Member
    Posted 3 years ago #

    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?

  2. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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;
    $wp->add_query_var('news');
    add_rewrite_rule('clubs/([^/]+)/news', 'index.php?club=$matches[1]&news=1', 'top');

    Going to:

    /clubs/manchester-united/news

    is equivalent to going to:

    /clubs/manchester-united/?news=1

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

    <?php
    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic