WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
WP_Query to display CPT post that have connections only (5 posts)

  1. dashaluna
    Member
    Posted 2 years ago #

    Hello,

    I've got posts2posts 1.1.3

    I've got two CPT that are connected. I'm constructing a new WP_Query to display one of the CPT as following:

    new WP_Query(array(
    		'post_type' => 'fsd-slider',
    		'order' => 'ASC',
    		'orderby' => 'menu_order',
    		'posts_per_page' => -1,
    	);
    );

    Is there a way to only select CPT posts that have been associated with another CPT?

    I've tried adding the connection name, for example 'connected_type' => 'slider_to_case_study' but that didn't help.

    I would really appreciate any help thanks!

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

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Besides 'connected_type', you also need to add 'connected_items' => 'any'.

  3. dashaluna
    Member
    Posted 2 years ago #

    @scribu, thank you for the reply.

    So, now I've got the following:

    new WP_Query(array(
    		'post_type' => 'fsd-slider',
    		'order' => 'ASC',
    		'orderby' => 'menu_order',
    		'posts_per_page' => -1,
    		'connected_type' => 'slider_to_case_study',
    		'connected_items' => 'any'
    	);
    );

    However, I think this results in incorrect query being generated:

    SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts
    WHERE 1=1
    AND YEAR(wp_posts.post_date)='2525'
    AND wp_posts.post_type = 'post'
    AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
    ORDER BY wp_posts.menu_order, wp_posts.post_date DESC
    LIMIT 0, 10

    There shouldn't be a LIMIT as posts_per_page is set too -1. Also, the year parameter suddenly appears.

    Any ideas what might be going wrong?

    Thanks in advance.

  4. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Ah, you also have to specify 'connected_direction', with a value of 'from', 'to', or 'any', depending on how you defined the connection type.

    This is a bug, though:

    https://github.com/scribu/wp-posts-to-posts/issues/94

  5. dashaluna
    Member
    Posted 2 years ago #

    Thanks for letting me know!
    Would be nice to know any updates on it.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic