0

The Pagination for my custom post type returns the same posts on every page. Like when hovered over the pagination like the link shows domain.com/android/page/2 but when clicked it returns to domain.com/android/ with the same content.

Here is my Custom Post Type Code:

register_post_type( 'Subject', array( 
    'capability_type' => 'Subject',
    'map_meta_cap' => true, 
    'show_in_rest' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'subject'),
    'has_archive' => true,
    'public' => true,
    'labels' => array(
        'name' => 'Subjects',
        'add_new_item' => 'Add new Subject',
        'edit_item' => 'Edit Subject',
        'all_items' => 'All Subjects',
        'singular_name' => 'Subject'
    ),
    'menu_icon' => 'dashicons-buddicons-topics',
    'supports' => array( 
        'title', 
        'editor',  
        'thumbnail', 
        'revisions'),
) );

register_post_type( 'Course', array(
    'capability_type' => 'Course',
    'map_meta_cap' => true, 
    'public' => true,
    'has_archive' => true,
    'labels' => array(
        'name' => 'Courses',
        'add_new_item' => 'Add new Course',
        'edit_item' => 'Edit Course',
        'all_items' => 'All Courses',
        'singular_name' => 'Course'
    ),
    'menu_icon' => 'dashicons-book',
    'supports' => array( 
        'title', 
        'editor', 
        'comments', 
        'revisions'),
) );

I am trying to get my Custom Post Type Subject to return all post related to Custom Post Type Course.

Here is my Single-subject.php Code:

<?php   
    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } 
    elseif ( get_query_var('page') ) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }
?>

$custom_query_args = array(
            'post_type' => 'course',
            'posts_per_page' => 2,
            'paged' => $paged,
            'meta_query' => array(
                array(
                    'key' => 'related_subject',
                    'compare' => 'LIKE',
                    'value' => '"'.get_the_ID().'"'
                )
            )
        );

$relatedCourses = new WP_Query( $custom_query_args );
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $relatedCourses;

while ($relatedCourses->have_posts()) {
             $relatedCourses->the_post();
        ?>
 Custom Post Type Display

<?php   } 
    wp_reset_postdata();
?>

<div>
      <?php echo (paginate_links()); 
      $wp_query = NULL;
      $wp_query = $temp_query;?>
    </div>

Have been searching Google but no luck. I need it Urgent. Help!

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.