• Resolved wp-MeMeMe

    (@wp-mememe)


    In an earlier thread, scribu posted this for counting connections:

    $connected = new WP_Query( array(
    	'post_type' => 'page',
    	'connected_from' => $post->ID
    ) );
    
    echo $connected->found_posts;

    I’ve been trying to use it in my single.php and it always returns zero. I have code which properly displays connections to other post types, so I know they exist, and are working. But the above snippet doesn’t detect them.

    My site uses custom post types ‘toys’, ‘stores’, ‘builders’
    I have a post of type ‘toys’ which has properly displayed connections to ‘stores’ and another connection to a ‘builders’ post.

    But these code snippets all produce an answer of zero:

    $connected = new WP_Query( array(
    	'post_type' => 'stores',
    	'connected_from' => $post->ID
    ) );
    echo $connected->found_posts;
    
    $connected = new WP_Query( array(
    	'post_type' => 'builders',
    	'connected_from' => $post->ID
    ) );
    echo $connected->found_posts;
    
    $connected = new WP_Query( array(
    	'post_type' => 'toys',
    	'connected_from' => $post->ID
    ) );
    echo $connected->found_posts;

    I’ve tried replacing $post->ID with get_queried_object() too, as well as ‘connected_from’ to ‘connected_to’ – I really have no idea what I’m doing!

    What I really need is to be able to tell if a given post has more than 0 connections of ANY type.

    https://www.ads-software.com/extend/plugins/posts-to-posts/

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

    (@scribu)

    That’s a really old snippet. Connection types have names.

    What should work now:

    $connected = new WP_Query( array(
    	'connected_type'  => 'YOUR_CONNECTION_TYPE_NAME',
    	'connected_items' => $post->ID,
    ) );
    
    echo $connected->found_posts;

    Note that $connected contains a (limited) list of posts too, not just their count.

    Thread Starter wp-MeMeMe

    (@wp-mememe)

    That does work! Thanks.

    I have to repeat this several times in the template for each connection, count the number and if the total is greater than 0, draw a styling block to contain the connection details.

    So, I tried to create a function in my core functionality plugin, like this:

    function swg_count_connections_single($post_id,$connection_type){
    	$connected = new WP_Query( array(
    			'connected_type'  => $connection_type,
    			'connected_items' => $post->ID,
    	) );
    	wp_reset_postdata(); // is this needed?
    	return $connected->found_posts;
    }

    and called it in the template (for testing purposes) like this:

    $swg_count_of_connections =  swg_count_connections_single($post->ID,'builders_to_toys');
    echo $swg_count_of_connections;

    This doesn’t work, it always returns zero. What am I missing?

    Plugin Author scribu

    (@scribu)

    You’re accepting $post_id as a function parameter, but you pass $post->ID to WP_Query.

    Thread Starter wp-MeMeMe

    (@wp-mememe)

    Gah! how did I miss that?

    Thanks!

    Note that $connected contains a (limited) list of posts too, not just their count.

    What does “limited” mean?

    Plugin Author scribu

    (@scribu)

    It means it only contains a page-worth of posts. To get all of them, you need to pass 'nopaging' => true.

    Thanks for the info!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘count connections not working’ is closed to new replies.