Forums

Posts 2 Posts
Adding a sub menu to a CPT list if a "relationship" exists (11 posts)

  1. jrstaatsiii
    Member
    Posted 5 months ago #

    I have a page that queries a custom post type called "Clients" that is organized by a taxonomy called "Industry Type." As an example, see the code below:

    <h2>Automotive</h2>
    <ul>
    	<?php
    	//Target The Client CPT & Taxonomy
    	$auto_args = array(
    	'post_type'	=> 	'client',
    	'order_by'		=>	'title',
    	'order'		=>	'ASC',
    	'posts_per_page'	=>	'-1',
    	'tax_query'	=> 	array(
    		array(
    			'taxonomy'	=>	'industry-type',
    			'field'		=>	'slug',
    			'terms'		=>	'automotive'
    		)
    	)
    );
    
    	//Setup The Query
    	$auto_query = new WP_Query($auto_args);
    
    	//The Loop
    		while($auto_query->have_posts()) : $auto_query->the_post();
    		?>
    
    			<li>
    			<?php the_title(); ?>
    			</li>
    
    		<?php endwhile; wp_reset_query(); ?>
    	</ul>
    
    <h2>Beauty/Fashion</h2>
    	<ul>
    	<?php
    	//Target The Client CPT & Taxonomy
    	$beauty_args = array(
    	'post_type'		=> 	'client',
    	'order_by'			=>	'title',
    	'order'			=>	'ASC',
    	'posts_per_page'	=>	'-1',
    	'tax_query'		=> 	array(
    	array(
    		'taxonomy'	=>	'industry-type',
    		'field'		=>	'slug',
    		'terms'			=>	'beauty-fashion'
    		)
    	)
    );
    
    //Setup The Query
    	$beauty_query = new WP_Query($beauty_args);
    
    	//The Loop
    	while($beauty_query->have_posts()) : $beauty_query->the_post();
    	?>
    
    	<li><?php the_title(); ?></li>
    
    	<?php endwhile; wp_reset_query(); ?>
    </ul>

    What I am trying to accomplish with Posts2Posts is check to see if any of the list items pulled from the query have a connection. Within WP, I have a connection in place between clients and case studies. If a client has a case study connected to it, I would like to add a submenu to that list item like so:

    <li>
    <?php the_title(); ?>
    	<ul class="connected">
    		<li>Connected Case Study</li>
    	</ul>
    </li>

    Truth be told, I am a bit vexed as to how to accomplish this, but I tried the following code from the basic usage tutorial with no luck:

    <h2>Technology</h2>
    <ul>
    	<?php
    	//Target The Client CPT & Taxonomy
    	$technology_args = array(
    		'post_type'		=> 	'client',
    		'order_by'			=>	'title',
    		'order'			=>	'ASC',
    		'posts_per_page'	=>	'-1',
    		'tax_query'		=> 	array(
    		array(
    			'taxonomy'	=>	'industry-type',
    			'field'		=>	'slug',
    			'terms'		=>	'technology'
    		)
    	)
    );
    
    	//Setup The Query
    	$technology_query = new WP_Query($technology_args);
    
    	//Check to see if there is a relationship between a client and a case study
    	p2p_type( 'clients_to_case_studies' )->each_connected( $wp_query );
    
    	//The Loop
    	while($technology_query->have_posts()) : $technology_query->the_post();
    	?>
    
    		<?php if($connected->have_posts()) { ?>
    
    		<li>
    
    		<?php the_title(); ?>
    			<ul class="connected">
    				<li>Testing...</li>
    			</ul>
    		</li>
    
    		<?php } else { ?>
    
    		<li><?php the_title(); ?></li>	
    
    		<?php } 
    
    	endwhile; wp_reset_query(); ?>
    
    </ul>

    This code just outputs a blank:
    <ul></ul>

    Any chance I could be pushed in the right direction. Or, a bit of info as to if this is even possible

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

  2. scribu
    Member
    Posted 5 months ago #

    It would be better if you pasted your code into http://gist.github.com or pastebin.com as it would be easier to read.

    Or maybe just remove those many extra tab levels.

  3. jrstaatsiii
    Member
    Posted 5 months ago #

    sorry about that. i just updated it with less tabbed spaces.

  4. scribu
    Member
    Posted 5 months ago #

    Firstly, you create a $technology_query variable, but you pass $wp_query to each_connected().

    Secondly, there's no $connected variable defined anywhere. So, instead of:

    if($connected->have_posts())

    you should have

    if ( !empty( $post->connected ) )

  5. jrstaatsiii
    Member
    Posted 5 months ago #

    Thanks for the help so far Scribu, I think I am close. Since I think this is definitely a premium ($$) plugin and since I don't believe in free support, I sent over a "premium" donation from rich at secretstache com.

    Here is where I am so far:

    p2p drury

    And this is looping through fine, however it is not showing a connection for the one client I have connected to a case study. This is how I have the initial p2p init:

    //Add Support for Post 2 Posts
    	function my_connection_types() {
    		// Make sure the Posts 2 Posts plugin is active.
    		if ( !function_exists( 'p2p_register_connection_type' ) )
    			return;
    
    		p2p_register_connection_type( array(
    			'name' => 'clients_to_case_studies',
    			'from' => 'client',
    			'to' => 'case-study'
    		) );
    	}
    	add_action( 'init', 'my_connection_types', 100 );

    and here is a screen shot of how there is a connection made within wordpress:

    twitpic

    I just combined both screen shots into one to see that the metabox appeared on both CPTs.

    Thanks again for all of your help.... this plugin is pretty sick.

    Rich

  6. scribu
    Member
    Posted 5 months ago #

    Here's what I did:

    1. Registered the post types in a minimal fashion:

    register_post_type( 'client', array( 'label' => 'Clients', 'public' => true ) );
    register_taxonomy( 'industry-type', 'client', array( 'label' => 'Industry Type' ) );
    
    register_post_type( 'case-study', array( 'label' => 'Case Studies', 'public' => true ) );

    and added your connection type.

    2. Created some posts and a connection, as indicated in your screenshot.

    3. Created a custom page template and pasted there the code exactly as it's shown in your gist.

    And it works as expected. So, there's something else going on.

    Try:

    * switching to your default theme
    * deactivating all other plugins

    Since I think this is definitely a premium ($$) plugin and since I don't believe in free support, I sent over a "premium" donation from rich at secretstache com.

    Received, thanks!

  7. jrstaatsiii
    Member
    Posted 5 months ago #

    I really quickly deactivate the few plugins i had running, and that was not the issue as far as i can tell. Before I go ahead and recreate a basic theme as you did, I wanted to point out that I am developing locally in a Multisite installation, and was wondering if that might be the problem. It is not network activated, but figured I would mention that if you knew of any MS issues.

    Rich

  8. jrstaatsiii
    Member
    Posted 5 months ago #

    The only other tidbits I think I should mention is that I have all of my custom post types in a "core functionality" plugin and i am building a child theme that uses Genesis as the parent.

    Rich

  9. scribu
    Member
    Posted 5 months ago #

    Before I go ahead and recreate a basic theme as you did

    To be clear, I didn't create a basic theme from scratch. I just created a custom page template inside the twentyeleven theme.

    I wanted to point out that I am developing locally in a Multisite installation, and was wondering if that might be the problem.

    There's no difference between single-site and multi-site operation.

    The only other tidbits I think I should mention is that I have all of my custom post types in a "core functionality" plugin

    That's a good idea. Just make sure there's no additional code in there that might be interfering.

  10. jrstaatsiii
    Member
    Posted 5 months ago #

    Hey Scribu,

    Sorry for the delay in this response. I was able to get it working, and here is what was causing the problem.

    I was using genesis as a parent theme, and was trying to call the posts2posts relationship above within the genesis custom loop. Within the custom loop I was not able to get posts2posts to work, but when I created a normal WP template that called the header, footer, sidebar, etc it worked fine. I think I was using the genesis custom loop improperly.

    Just thought you would like to know that I got it working, and that it was a problem with inserting the code within the custom loop.

    Rich

  11. scribu
    Member
    Posted 5 months ago #

    Thanks for the follow-up.

Reply

You must log in to post.

About this Plugin

About this Topic