WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Looping the loop help (8 posts)

  1. Griffin
    Member
    Posted 2 years ago #

    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/

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    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.

  3. Griffin
    Member
    Posted 2 years ago #

    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!

  4. Griffin
    Member
    Posted 2 years ago #

    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!

  5. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    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)

  6. Griffin
    Member
    Posted 2 years ago #

    Thank you! I'll give it a try.

  7. Griffin
    Member
    Posted 2 years ago #

    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.

  8. scribu
    Member
    Plugin Author

    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic