0

I have a custom post type called chapter. I'm trying to show the next chapter with something like the get_next_post_link() and get_previous_post_link(). I get the next chapter that is posted, but I only want the post that share the same value of a custom field. meta_key = 'en_chapter_story'

I've seen topics talking about ordering the post by the custom field value for that, but I just want to limit the results by those that share the same custom field value.

I've also tried a get_posts() request:

  $get_chapter = get_post_meta( $post->ID, 'en_chapter_story',true );

         $chapter_args = array(
            'post_type'     => 'chapter',
            'meta_key'      => 'en_chapter_story',
            'meta_value'    => $get_chapter,
            'posts_per_page' => 1,
        );
    if( $chapter = get_posts($chapter_args ) ) {
        foreach( $chapter as $post ) { 
            setup_postdata( $post );
            echo the_title();
            echo '<br>';

        }}
            wp_reset_postdata(); 

but I don't know how to tell the get(); to look for the post published right before or after the current one.

Also if it helps the slug of each of the custom post type will be incremented by one. Example lk1-1-00000-01, lk1-1-000004-02, lk1-1-000004-03, etc.

the code for the chapter template:

 <?php
 /**
 * The template part for displaying single chapters
 * @package WordPress
 * @subpackage Twenty_Sixteen
 * @since Twenty Sixteen 1.0
 */
 ?>
 <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 <div>
 <?php $get_story = get_post_meta( $post->ID, 'en_chapter_story',true );
      $story_args = array(
        'post_type'        => 'product',
        'name'             => $get_story,
        );
    if( $story = get_posts($story_args ) ) {
        foreach( $story as $post ) { 
            setup_postdata( $post );
        ?>
       <?php if ( has_post_thumbnail() ) :{?>
            <a href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
                <img src="<?php the_post_thumbnail_url('medium'); ?>" style="float: left; margin: 10px 10px 0 0;"/>
            </a>

            <?php } else : { ?>
                <img src="http://enspirepublishing.com/forbiddenfiction/wp-content/uploads/sites/2/2017/10/ForbiddenFic_CvrPlaceholderSM.png" style="float: left; margin: 10px 10px 0 0;"/>
            <?php }
            endif; ?>

        <a href="<?php the_permalink(); ?>"><?php the_title('<b style="font-size: 2em;">', '</b>'); ?></a>
        <br>
            <?php 
            if ( function_exists( 'coauthors_posts_links' ) ) {
                    echo 'by ';
                    coauthors_posts_links();
                     } 
                    else {
                    the_author_post_link();
                    }?>
            <br><br>
        <?php the_content(); ?>
    <?php } }
    wp_reset_postdata(); ?>
    </div>
    <div align="center">
         <?php 
         $get_chapter = get_post_meta( $post->ID, 'en_chapter_story',true );

         $chapter_args = array(
            'post_type'     => 'chapter',
            'meta_key'      => 'en_chapter_story',
            'meta_value'    => $get_chapter,
            'posts_per_page' => 1,
        );
    if( $chapter = get_posts($chapter_args ) ) {
        foreach( $chapter as $post ) { 
            setup_postdata( $post );
            echo the_title();
            echo '<br>';

        }}
            wp_reset_postdata(); 
            global $post;
            $chapter_slug = $post->post_name;
            echo $chapter_slug;
            $next_chapter = $chapter_slug++;
            echo 'Test: ';
            echo $next_chapter;

        ?>
        <?php the_title('<h2>', '</h2>'); ?>
        <br>

        <?php the_content(); ?>
        <?php 
        echo '<style>table, th, td {border: 0px;}</style><table><tr><td>' ;
        if  ( is_singular('chapter')  ) {previous_post_link( '%link', '< Previous Chapter' );}
        echo '</td><td style="text-align:right">' ;
        if ( is_singular('chapter') ) {next_post_link( '%link', 'Next Chapter >' );} 
        echo '</td></tr></table>' ;
        ?>
    </div>

  • Can you add the whole code for the template? – Kumar Oct 28 '17 at 5:03
  • I added the code for the chapter template to my question. – Kaye O'Malley Oct 28 '17 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.