• Resolved Nick Davis

    (@esanctuary)


    Hello, as I have mentioned before I love this plugin but I am having a small issue gettng it to work ok within a category/archive/similar loop

    I have read the recent tutorial on Scribu’s website (https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop) but I’m coming unstuck and unable to produce any results.

    I have two (custom) post types:

    – Shop
    – Offer

    I have successfully related these to each other and am able to display them on the appropriate single-xxx.php pages, however I also wanted to list the appropriate relationships in any appropriate category/taxonomy etc loops.

    This is the code I am using for the output:

    if ( !empty( $post->connected_pages ) ) {
    		echo '<h5>Connected Pages:</h5>';
    		echo '<ul>';
    
    		foreach ( $post->connected_pages as $post ) {
    			setup_postdata( $post );
    
    			echo '<li>';
    			the_title();
    			echo '</li>';
    		}
    
    		wp_reset_postdata();
    
    		echo '</ul>';
    	}

    However I am thinking that is not the issue, it’s the following bit of code I’m not sure about:

    p2p_each_connected( 'any', 'pages', array(
    	'post_type' => 'shop',
    	'nopaging' => true,
    ) );

    I have tried putting this just above the loop, in the loop and in functions.php and I’m not sure I am doing this correctly at all, because I am getting no results (I have also tried ‘offer’ as the post_type and various other array/parameter combinations).

    Nothing breaks on the page, I just don’t get any results either

    Would really appreciate any help you can give. I

    Thanks

    Nick

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

    (@scribu)

    It would be useful to post the entire template file, using https://pastebin.com

    PS: p2p_each_connected() goes before The Loop.

    Thread Starter Nick Davis

    (@esanctuary)

    Thanks Scribu, I’m a bit of a novice (but hopefully learning fast and have been able to achieve everything I wanted to do up to this point) so please excuse any horrors in the code!

    https://pastebin.com/L11si6U1

    Thread Starter Nick Davis

    (@esanctuary)

    Sorry, I should’ve said it’s the second loop I’m looking at here (the second half of the code)

    Plugin Author scribu

    (@scribu)

    Ah, well the problem is that p2p_each_connected() only affects the global WP_Query instance.

    To handle custom instances, you would replace this:

    $query = new WP_Query( array( 'malls' => 'dubai-mall', 'post_type' => 'shop' ) );
    
    p2p_each_connected( 'any', 'offers', array(
            'post_type' => 'offer',
            'nopaging' => true,
    ) );

    with this:

    $query = new WP_Query( array(
      'malls' => 'dubai-mall',
      'post_type' => 'shop',
      'each_connected' => array(
            'post_type' => 'offer',
            'nopaging' => true,
      )
    ) );

    and instead of $post->connected_offers, you would use $post->connected.

    Thread Starter Nick Davis

    (@esanctuary)

    Thanks, I updated the code, however it still doesn’t quite work (probably me, rather than your code), I think it must be in the last section of the code which now reads:

    if ( !empty( $post->connected) ) {
    		echo '<h5>Connected Pages:</h5>';
    		echo '<ul>';
    
    		foreach ( $post->connected as $offer ) {
    			setup_postdata( $offer );
    
    			echo '<li>';
    			the_title();
    			echo '</li>';
    		}
    
    		wp_reset_postdata();
    
    		echo '</ul>';
    	}

    (I also tried a few other variations based on the code here: https://scribu.net/wordpress/posts-to-posts but couldn’t get any output unfortunately)

    I have pasted the whole code again here in case it helps (again it’s the second part only): https://pastebin.com/FT0KPbjB

    I am sorry to keep bugging you about this one but do you have any other suggestions I could try? Must be almost there!

    Thanks, I really appreciate your help

    Nick

    Plugin Author scribu

    (@scribu)

    It looks ok now. The question is why you have almost the same code twice.

    Remove the first part and you should be able to more easily see what the problem might be.

    Thread Starter Nick Davis

    (@esanctuary)

    Thanks, I’ve tried a few things since your last post, some close to what you recommended, others further away, the closest I have been able to get so far is here:

    https://pastebin.com/mc8QbDjM

    Where for the first time it does display the relation, however it shows it on all posts instead of just the one that has the relation

    I will keep trying but just thought I’d give you an update, as this seems to be getting closer at least

    Thanks

    Nick

    Plugin Author scribu

    (@scribu)

    Try calling wp_reset_postdata(); just before endwhile; too.

    Thread Starter Nick Davis

    (@esanctuary)

    Hi Scribu

    Thanks for all your help, just wanted to say I managed to do this in the end by using your updated tutorial on this page:

    https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop

    (Also posted for anyone else who comes across the same thing in future)

    Thanks again

    Nick

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Unable to display relations in a category/taxonomy/archive page loop’ is closed to new replies.