wp-posts-to-posts icon indicating copy to clipboard operation
wp-posts-to-posts copied to clipboard

Use with Timber?

Open swthate opened this issue 9 years ago • 10 comments

Has anyone used this wonderful plugin with Timber? If so, how exactly?

swthate avatar Feb 23 '16 17:02 swthate

I've used it extensively with Timber. Usually I extend TimberPost and add methods for getting connected content ( ExtendedPost::get_events() for connected events, for example).

Lately I've been looking around for alternatives to Posts to Posts, since it basically hasn't been maintained for a few years and is getting pretty buggy. So far nothing else has come close, unfortunately.

chrisgherbert avatar Feb 23 '16 18:02 chrisgherbert

Sorry, but do you have any working examples I could scour? I'm pretty darn new to Timber and feel confused/overwhelmed!

swthate avatar Feb 23 '16 20:02 swthate

Class

class ExtendedTimber extends TimberPost {

    public function get_connected_posts(){

        $query = array(
            'connected_type' => YOUR_CONNECTION_TYPE,
            'connected_items' => array($this->ID),
            'nopaging' => true
        );

        return Timber::get_posts($query);

    }

    // Aliases

    public function connected_posts(){
        return $this->get_connected_posts();
    }

}

Controller (ie, single.php or similar)

$posts = Timber::get_posts(false, 'ExtendedTimber');
$context['posts'] = $posts;

Template

{% for connected_post in post.connected_posts %}
    Title: {{ connected_post.title }}
{% endfor %}

chrisgherbert avatar Feb 23 '16 20:02 chrisgherbert

Awesome, thank you so much! I think I'll be abel to get it now.

swthate avatar Feb 23 '16 22:02 swthate

Great, glad I could help. Basically, I'm moving the heavy lifting of getting the connected posts into the model, rather than sticking it in the controller (bad) or the view (worse).

chrisgherbert avatar Feb 23 '16 22:02 chrisgherbert

I'm guessing the "model" or first chunk of code you listed is supposed to be put in functions.php?

swthate avatar Feb 23 '16 22:02 swthate

It could go there, though I'd probably make a separate class for each post type that you're using, and then keep them all in separate files and include them.

chrisgherbert avatar Feb 23 '16 22:02 chrisgherbert

Hmm, I can't seem to get it to work. Pretty much everything is the same. Here's my 'model':

function connect_job_to_client() {
  p2p_register_connection_type( array(
    'name' => 'job_to_client',
    'from' => 'jobs',
    'to'   => 'clients',
    'admin_box' => array(
      'show'    => 'any',
      'context' => 'side',
    )
  ) );
}
add_action( 'p2p_init', 'connect_job_to_client' );


class ExtendedTimber extends TimberPost {
  public function get_connected_posts(){
    $query = array(
      'connected_type'  => connect_job_to_client(),
      'connected_items' => array($this->ID),
      'nopaging'        => true
    );
    return Timber::get_posts($query);
  }
  // Aliases
  public function connected_posts(){
    return $this->get_connected_posts();
  }
}

I wasn't sure where to put the connect_job_to_client function, figured right above the ExtendTimber class was the most obvious place.

I also wasn't sure what to put for 'connected_type', as at first simply 'job_to_client' didn't work.

swthate avatar Feb 23 '16 22:02 swthate

Here's my 'controller' (single.php)

$context = Timber::get_context();
$post = Timber::get_posts(false, 'ExtendedTimber');
$context['post'] = $post;


Timber::render( array( 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ), $context );

swthate avatar Feb 23 '16 22:02 swthate

The connected_type item in the array is looking for the name of the connection type - job_to_client in this case. That's a string, not a function. This part works the same as using Posts 2 Posts outside of Timber, so make sure you're familiar with that.

The "Basic Usage" page in the p2p docs should cover it pretty well: https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage

if you've already tried job_to_client, then the problem lies elsewhere. Check your logs, and make sure that you're actually getting an ExtendedTimber object from Timber::get_posts

chrisgherbert avatar Feb 23 '16 22:02 chrisgherbert