Display metabox conditionally

Of course… function generic_cb($post) { echo ‘This is a test of the emergency callback system.’; } add_action( ‘add_meta_boxes_page’, ‘conditional_meta_box’ ); function conditional_meta_box($post) { $meta = get_post_meta($post->ID,’meta_key’,true); if (‘abc’ === $meta) { add_meta_box( ’emcb’, ‘CB Test’, ‘generic_cb’, ‘page’, ‘normal’, ‘high’ ); } } The add_meta_boxes* hooks pass the $post variable, so use it to check the … Read more

Pagination in plugin with custom post type

the_posts_pagination uses global query and global query does not have pagination. So it is the correct behavior of WordPress. To overcome from this problem assign custom query to global query then after looping again restore the global query. function foo($args){ $post_type = $args[‘post_type’]; global $wp_query; $original_query = $wp_query; $custom_post_type = new WP_Query( array( ‘post_type’ => … Read more

Pagination problems with multiple custom post type archive pages

Your problem comes from a fundamental misunderstanding of how WordPress loads the main loop. Here you have a main query that goes and grabs the posts to display in your post type archive. It then decides to load archive-recept.php based on that query. The call to paginate_links then provides the pagination for that main query. … Read more

How to show related posts by category or custom post type?

<?php $related = get_posts( array( ‘category__in’ => wp_get_post_categories($post->ID), ‘numberposts’ => 5,’post_type’ => ‘news’, ‘post__not_in’ => array($post->ID) ) ); if( $related ) foreach( $related as $post ) { setup_postdata($post); ?> <li> <?php the_post_thumbnail(); ?> <a href=”https://wordpress.stackexchange.com/questions/151897/<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a> </li> <?php } wp_reset_postdata(); ?>