WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Get connected posts by status? (8 posts)

  1. Patrick Daly
    Member
    Posted 2 years ago #

    Is there any way to check the status of the connected post and query by the status? I added my own line below, "connected_status", as an example.

    $connected = new WP_Query( array(
         'connected_type' => 'my_connection_name',
         'connected_items' => 'any',
         'connected_direction' => 'from',
         'connected_status' => array( 'private', 'draft' ),
         'post_type' => 'my_post_type'
    ));

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

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    I'm not sure what 'connected_status' would do. Do you mean get the posts grouped by status, as an associative array?

  3. Patrick Daly
    Member
    Posted 2 years ago #

    Not grouped, but just like any other WP query using 'post_status'.

    $connected = new WP_Query( array(
         'connected_type' => 'my_connection_name',
         'connected_items' => 'any',
         'connected_direction' => 'from',
         'post_type' => 'my_post_type'
    ));

    If I used 'post_status' in that query it would get the status for 'my_post_type' posts, but I want to get the status of the connected posts and only show the 'private' posts.

  4. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    So, if you had posts and pages, you want to get the pages that are connected to private posts, yeah?

    This is not currently possible without diving into the SQL. Could you give the me the actual use-case for this?

  5. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    You can do it like this:

    $connected_posts = get_posts( array(
         'post_status' => 'private',
         'connected_type' => 'posts_to_pages',
         'connected_items' => 'any',
         'connected_direction' => 'from',
         'suppress_filters' => false
    ) );
    
    $connected_pages = get_posts( array(
         'post_type' => 'page',
         'post__in' => wp_list_pluck( $connected_posts, 'p2p_to' ),
    ) );

    And I think that's good enough. You have the flexibility to use all the query vars that WP_Query accepts.

    Depending on your connection type, you might have to replace 'p2p_to' with 'p2p_from'.

    Obviously, you can use WP_Query instead of get_posts() if you need to.

  6. Patrick Daly
    Member
    Posted 2 years ago #

    Yeah, you understand it correctly.

    My site (guessages.com) uses 2 custom post types, "guess" and "photo". Users guess the age of photos that other users have submitted. Your plugin connects the guesses to the photos.

    Every photo and every guess submitted I create as private posts, so they're only available to the logged in user unless I override that.

    When a user submits their photos they can choose to make it active, putting it in the pool of photos to be guessed by everyone, but anytime they want to revert and make it inaccessible I have to change the post status. As the plugin stands, I can't differentiate between whether to display a photo in the main pool or not (without using meta or custom SQL, of course).

    I guess the tricky thing for you to decide is if you want to add something like this and where to draw the line. You could create almost every parameter WP_Query offers prefaced by "connected_". I could also stand to benefit from "connected_author". When querying for guesses that the current logged in user made I have to use meta, like so:

    'connected_meta' => array(
         array(
              'key' => 'guesser_id',
              'value' => $current_user->ID,
              'compare' => '=',
              'type' => 'numeric'
    	)
    )

    ... it would be much easier to use this:

    'connected_author' => $current_user->ID

  7. Patrick Daly
    Member
    Posted 2 years ago #

    I didn't see your last reply before I submitted. I think your method might work, so let me try it out.

  8. Patrick Daly
    Member
    Posted 2 years ago #

    wp_list_pluck wasn't working for me, but I ended up using this method.

    In English, first I query for all the photos that the current users has guessed. I pull out all of the 'p2p_to' IDs pointing at guess ID. Then, I query for all guesses, excluding all the IDs from the previous query.

    Thanks for your help, man.

    [33 lines of code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic