I am using the Posts 2 Posts plugin to related 2 custom post types, "Issues" and "Articles". "Articles" has a custom taxonomy of "genre". On each post's page I want to include a sidebar that has a table of contents.
On the single-issue.php page it needs to show all connected articles in an unordered list and by category.
like so:
Genre 1
-article 1
-article 2
Genre 2
-article 3
-article 4
Same thing for single-article.php, but it needs to only show articles from the same issue that the current article is connected to. it must show all posts including itself.
I will use the code in my sidebar template
jazbek answers:
Hi draivika,
Try this code:
// NOTE, please choose one of the following 2 lines of code to define the $issue variable:
// if you are on an issue page, use this line:
$issue = get_queried_object();
// if you are on an article page, use this line:
$issue = get_posts(array(
'connected_to' => get_queried_object(),
));
// code below this comment should go on both the issue and the article template:
// Gets every term in the genre taxonomy
$genres = get_terms('genre');
foreach($genres as $genre)
{
$articles = new WP_Query(array(
'taxonomy' => 'genre',
'term' => $genre->slug,
'posts_per_page' => -1,
'connected_type' => 'your_p2p_connection_type', // replace this with the name of your defined connection
'connected_to' => $issue,
));
if($articles->have_posts()): ?>
<h3><?php echo $genre->name ?></h3>
<ul>
<?php while( $articles->have_posts() ) : $articles->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
}
draivika comments:
Hey thanks. It looks like what I need, but I am getting a n error when I try to implement. I'm sure I am missing something with the loop here. Entire page template below:
<?php get_header(); ?>
<div id="content">
<div id="inner-content" class="wrap clearfix">
<div id="sidebar1" class="sidebar fourcol last clearfix" role="complementary">
<?php
$issue = get_queried_object();
// Gets every term in the genre taxonomy
$genres = get_terms('genre');
foreach($genres as $genre)
{
$articles = new WP_Query(array(
'taxonomy' => 'genre',
'term' => $genre->slug,
'posts_per_page' => -1,
'connected_type' => 'issues_to_content', // replace this with the name of your defined connection
'connected_to' => $issue,
));
if($articles->have_posts()): ?>
<h3><?php echo $genre->name ?></h3>
<ul>
<?php while( $articles->have_posts() ) : $articles->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
}
</div>
<div id="main" class="eightcol first clearfix" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header class="article-header">
</header> <!-- end article header -->
<section class="entry-content clearfix">
<?php the_content(); ?>
</section> <!-- end article section -->
<footer class="article-header">
</footer> <!-- end article footer -->
<?php comments_template(); ?>
</article> <!-- end article -->
<?php endwhile; ?>
<?php else : ?>
<article id="post-not-found" class="hentry clearfix">
<header class="article-header">
<h1><?php _e("Oops, Post Not Found!", "bonestheme"); ?></h1>
</header>
<section class="entry-content">
<p><?php _e("Uh Oh. Something is missing. Try double checking things.", "bonestheme"); ?></p>
</section>
<footer class="article-footer">
<p><?php _e("This is the error message in the page-custom.php template.", "bonestheme"); ?></p>
</footer>
</article>
<?php endif; ?>
</div> <!-- end #main -->
</div> <!-- end #inner-content -->
</div> <!-- end #content -->
<?php get_footer(); ?>
jazbek comments:
Apologies, please remove the closing ?> after the last "endif;" in my code.
jazbek comments:
You will also need to add a ?> at the end of my block of code (apologies for multiple comments, but this site gave me a server error when I tried to edit my previous comment).
draivika comments:
Thanks Jazbek, this looks like it should work and now there is no error, but I'm not getting any results. Am I supposed to add any other text? Am I supposed to add an actual slug name here? 'term' => $genre->slug,
Here is my edited code with my connection type incase I still have something wrong
<?php
$issue = get_queried_object();
// Gets every term in the genre taxonomy
$genres = get_terms('genre');
foreach($genres as $genre)
{
$articles = new WP_Query(array(
'taxonomy' => 'genre',
'term' => $genre->slug,
'posts_per_page' => -1,
'connected_type' => 'issues_to_content', // replace this with the name of your defined connection
'connected_to' => $issue,
));
if($articles->have_posts()): ?>
<h3><?php echo $genre->name ?></h3>
<ul>
<?php while( $articles->have_posts() ) : $articles->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif;
}
?>
jazbek comments:
Try changing the argument in the get_posts query form 'connected_to' to 'connected_from'. I do not think you need to do this with posts to posts, but you can also try adding the post type to the query, so:
$articles = new WP_Query(array(
'taxonomy' => 'genre',
'term' => $genre->slug,
'posts_per_page' => -1,
'connected_type' => 'issues_to_content', // replace this with the name of your defined connection
'connected_from' => $issue,
'post_type' => 'article',
));
You do not need to change the $genre->slug line.
draivika comments:
Yay! The issue page works!
The article page gets no results. actually the previous code before your last comment was returning all of the posts no mater the category, now with the updated code, no results.
Closer...Really appreciate this.
jazbek comments:
On the articles page, change the code that sets the $issue variable to this:
$issue = get_posts(array(
'connected_from' => get_queried_object(),
));
In my original code, I had assumed your connection type was set up so that articles were connected to issues, when in fact you have declared your connection so that issues are connected to articles (something I've always found to be a major point of confusion in the Posts to Posts plugin).. so you just need to switch the "connected_.." query vars from my original code. :)
jazbek comments:
AND if that alone doesn't work, please try adding 'post_type' => 'issue' to the arguments. :)
draivika comments:
Tried this still no results:
$articles = new WP_Query(array(
'taxonomy' => 'genre',
'term' => $genre->slug,
'posts_per_page' => -1,
'connected_type' => 'issues_to_content',
'connected_from' => $issue,
'post_type' => 'issue'
));
Here is my P2P function:
function my_connection_types() {
p2p_register_connection_type( array(
'name' => 'issues_to_content',
'from' => 'issue',
'to' => 'article',
'reciprocal' => true
) );
}
add_action( 'p2p_init', 'my_connection_types' );
I thought the "reciprocal" meant it didn't matter the direction.
jazbek comments:
It appears that you've edited the $articles query, you should be editing the $issue query (at the top of my block of code, where the $issue variable is set).
draivika comments:
Thanks for your patience,
Here is what I have on article page, no results.
<?php
$issue = get_posts(array(
'connected_from' => get_queried_object(),
'post_type' => 'issue'
));
// Gets every term in the genre taxonomy
$genres = get_terms('genre');
foreach($genres as $genre)
{
$articles = new WP_Query(array(
'taxonomy' => 'genre',
'term' => $genre->slug,
'posts_per_page' => -1,
'connected_type' => 'issues_to_content',
'connected_from' => $issue,
'post_type' => 'article',
));
if($articles->have_posts()): ?>
<h3><?php echo $genre->name ?></h3>
<ul>
<?php while( $articles->have_posts() ) : $articles->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif;
}
?>
jazbek comments:
OK! I think I've figured it out, thanks for bearing with me. I took your code and set it up in my own install. This worked on the article page:
$issue = get_posts(array(
'connected_to' => get_queried_object(),
'connected_type' => 'issues_to_content',
));
It was missing the 'connected_type' argument. And, note that you do not need the 'post_type' argument after all.
draivika comments:
Booya! works.
I'll probably have some other follow ups on this, but I'll just post them as new questions. This was totally worth it.
Thanks again.
jazbek comments:
Cheers, glad I was able to help.
draivika comments:
Just realized I want to show the name of the issue being viewed above everything. Is that a complicated thing to add to this answer?
Arnav Joy answers:
try this
<?php
$post_type = 'Articles';
$tax = 'genre';
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$tax_terms = get_terms( $tax , array( 'hide_empty' => 0 ) );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<ul class="taxlist">
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; // end of loop ?>
</ul>
<?php else : ?>
<?php endif; // if have_posts()
wp_reset_query();
} // end foreach #tax_terms
}
?>
draivika comments:
where does this incorporate the Posts 2 Posts relationship?
Arnav Joy comments:
sorry I did not get your point