Support » Plugin: Posts 2 Posts » [Plugin: Posts 2 Posts] Looping the loop help

  • Resolved Griffin

    (@rgriffinj)


    First, let me thank you for your wonderful plugin! You’re helping push the limits of WordPress as a CMS.

    Here’s my question:

    1. I have two custom post types: ‘Clients’ and “Placements”.
    2. I’m using your plugin to connect ‘Placements’ to ‘Clients’, which is working like a charm.
    3. Clients are divided into three industry groups using the value of a custom field(I’m using a custom field instead of a taxonomy for unrelated reasons).
    4. I have a page with three columns, each representing one of the three groups of ‘Clients’. In each column, I need to display all the ‘Placements’ that are associated with ‘Clients’ in that industry group.

    Is this possible using your plugin?

    Thank you!

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author scribu

    (@scribu)

    You can pass any additional query vars to WP_Query:

    $my_query = new WP_Query( array(
      'connected_type' => 'YOUR_CONNECTION_TYPE',
      'connected_items' => $YOUR_CLIENT_ID,
      'meta_key' => 'YOUR_META_KEY',
      'meta_value' => 'YOUR_META_VALUE',
    ) );
    
    while ( $my_query->have_posts() ) : $my_query->the_posts();
    
    ...

    And you do that for each column, only changing the meta_value.

    Thanks for the swift response! I had originally tried exactly what you posted, but it wasn’t working. Scanning through my code, however, I found a small typo that was causing the error. It’s working great now.

    Thank you!

    Hi!
    So this is working great now. I still have one issue, however. ‘Placements’ are now organized into groups based on what ‘Client’ they are associated with, so they are ordered chronologically within groups, and then each group is ordered chronologically with each other. The problem is that I need to order all placements chronologically with one another, independent of what ‘Client’ they are associated with. In other words, I need my loop to output just a list of ‘Placements’, instead of ‘Placements’ grouped by ‘Client’.

    Let me know if I need to clarify the question further.

    And thank you!

    Plugin Author scribu

    (@scribu)

    In that case, you don’t need to loop the loop at all.

    Just pass all the client ids to the ‘connected_items’ parameter as an array:

    $my_query = new WP_Query( array(
      'connected_type' => 'YOUR_CONNECTION_TYPE',
      'connected_items' => wp_list_pluck( $wp_query->posts, 'ID' ),
    ) );
    
    while ( $my_query->have_posts() ) : $my_query->the_posts();

    (assuming $wp_query->posts contains a list of ‘client’ posts)

    Thank you! I’ll give it a try.

    Worked perfectly. Thank you! This plugin is great and you’ve been extremely responsive. Do you have a donate button somewhere? I’d love to buy you a beer.

    Plugin Author scribu

    (@scribu)

    I have a donate button in the sidebar: http://scribu.net/wordpress/posts-to-posts

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Plugin: Posts 2 Posts] Looping the loop help’ is closed to new replies.