Forums

Posts 2 Posts
Querying connections via URL params (3 posts)

  1. reneelung
    Member
    Posted 8 months ago #

    I have having trouble querying posts using parameters passed in the URL.

    I have two custom post types I am connecting reciprocally, Athletes and Sports. For testing, I print out 1) the actual Sport ID, 2) the title and IDs of the athletes that are connected to that sport. This is done using the code:

    $athletes_query = new WP_Query(array('post_type' => 'athletes', 'connected_to' => get_queried_object_id()));
        echo get_queried_object_id();
        echo $post->ID;
        echo "<br/>";
        foreach ($athletes_query->posts as $athlete) {
            echo $athlete->post_name."-";
            echo $athlete->ID.", ";
        }

    However, when I try to perform the same query via URL (for this example, the sport I'm looking at has a post ID of 87),
    http://mysite.com/?post_type=athletes&connected_to=87
    I am given all the athletes, not just the ones I connected to the sport.

    What am I doing wrong here?

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Posted 8 months ago #

    For query variables to be used directly from the URL, they need to be registered, like so:

    function make_p2p_qv_public() {
        global $wp;
    
        $wp->add_query_var( 'connected_to' );
    }
    
    add_action( 'init', 'make_p2p_qv_public' );
  3. reneelung
    Member
    Posted 8 months ago #

    Thanks for getting back to me so quickly!

    I tried your snippet, but it didn't seem to work. I inserted it into my functions.php file, logged out and logged back in just be sure, but I'm still getting all the athletes.

    I also tried the query_vars hook, but that didn't seem to work either.

    function add_p2p_qv ($public_query_vars) {
        $public_query_vars[] = 'connected_to';
        return $public_query_vars;
    }
    
    add_action( 'query_vars', 'add_p2p_qv' );

    People seem to think a parse_request filter is also needed, but I'm not familiar enough with hooks and filters to tell.

    Any suggestions?

Reply

You must log in to post.

About this Plugin

About this Topic