I am trying to create a custom post type with taxonomy and a shortcode for my custom search in a plugin.When I activate my plugin I see my CPT and taxonomy on dashboard. But when I publish the post I do not see the data in form of the single-{CPT}.php page I created. Instead, it displays list of all posts from my CPT. The relevent files are as below.
index.php (my plugin page)
function register_vfic_post_type() {
/* ----Begin Code to include Custom Post Types and its Taxonomies----- */
register_post_type('vfic_publications', array( 'label' => 'VFIC Publications','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => false,'rewrite' => array('slug' => ''),'query_var' => true,'exclude_from_search' => false,'supports' => array('title','editor','excerpt','revisions','author','page-attributes',),'labels' => array (
'name' => 'VFIC Publications',
'singular_name' => '',
'menu_name' => 'VFIC Publications',
'add_new' => 'Add VFIC Publications',
'add_new_item' => 'Add New VFIC Publications',
'edit' => 'Edit',
'edit_item' => 'Edit VFIC Publications',
'new_item' => 'New VFIC Publications',
'view' => 'View VFIC Publications',
'view_item' => 'View VFIC Publications',
'search_items' => 'Search VFIC Publications',
'not_found' => 'No VFIC Publications Found',
'not_found_in_trash' => 'No VFIC Publications Found in Trash',
'parent' => 'Parent VFIC Publications',
),) );
flush_rewrite_rules();
}
add_action( 'init', 'register_vfic_post_type' );
register_taxonomy('Year',array (
0 => 'vfic_publications',
),array( 'hierarchical' => false, 'label' => 'Year','show_ui' => true,'query_var' => true,'rewrite' => false,'singular_label' => 'Year') );
register_taxonomy('PubsAuthor',array (
0 => 'vfic_publications',
),array( 'hierarchical' => false, 'label' => 'PubsAuthor','show_ui' => true,'query_var' => true,'rewrite' => false,'singular_label' => 'PubsAuthor') );
register_taxonomy('Journal',array (
0 => 'vfic_publications',
),array( 'hierarchical' => false, 'label' => 'Journal','show_ui' => true,'query_var' => true,'rewrite' => false,'singular_label' => 'Journal') );
register_taxonomy('nts',array (
0 => 'vfic_publications',
),array( 'hierarchical' => false, 'label' => 'Non-Technical Summary','show_ui' => true,'query_var' => true,'rewrite' => false,'singular_label' => 'nts') );
function vfic_ctg_rewrite_flush() {
register_vfic_post_type();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'vfic_ctg_rewrite_flush' );
/* ----END Code to include Custom Post Types and its Taxonomies----- */
function vfic_pubs_forms_shortcode() { ?>
<div class="pub-search-form">
<form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<p>Keyword(s): <input type="text" class="field" name="s" value="" id="s" class="s" />
<input type="checkbox" name="abstract" value=""/>Just Search Abstract</p>
<p>Author(s):    <?php $myterms = get_terms(array('PubsAuthor'), array('order'=>'ASC','hide_empty'=>true));
$author = "pubsauthors";
$emptyvalue = "";
$text = "text";
$term_taxonomy=$term->PubsAuthor;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<input type='".$text."' name='".$link."'/>";
echo $output; ?>
<?php $myterms = get_terms(array('PubsAuthor'), array('order'=>'ASC','hide_empty'=>true));
$author = "pubsauthor";
$emptyvalue = "";
$output ="<select name='".$author."'><option selected='".$selected."' value='".$emptyvalue."'>--Select Authors--</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->PubsAuthor;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
echo $output;
?></p>
<p>Year:            
<?php $myterms = get_terms(array('Year'), array('order'=>'ASC','hide_empty'=>true));
$year = "pubsyear";
$emptyvalue = "";
$output ="<select name='".$year."'><option selected='".$selected."' value='".$emptyvalue."'>--Select Year--</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->Year;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
echo $output;
?></p>
<input type="hidden" name="post_type" value="vfic_publications" />
<input type="submit" id="submit" value="Search" />
</form>
</div>
<?php } ?>
<?php add_shortcode('vfic_pubs_search', 'vfic_pubs_forms_shortcode');
add_filter( "template_redirect", "get_custom_post_type_template" ) ;?>
<?php function get_custom_post_type_template($template_redirect) {
global $post;
if ($post->post_type == 'vfic_publications') {
$template_redirect = get_template_part('loop','vfic_publications');
}
return $template_redirect;
} ?>
loop-{custom post type}.php page
get_header(); ?>
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$search = new WP_Query($search_query);
$author = $_GET['pubsauthor'];
$abstract = $_GET['abstract'];
$tax_query = array('relation' => 'OR');
if ($_GET['pubsauthor'])
{
$tax_query[] = array(
'taxonomy' => 'PubsAuthor',
'field' => 'slug',
'terms' => array($_GET['pubsauthor'])
);
}
if ($_GET['pubsyear'])
{
$tax_query[] = array(
'taxonomy' => 'Year',
'field' => 'slug',
'terms' => array($_GET['pubsyear'])
);
}
$query = new WP_Query(
array(
//Retreive ALL publications posts
'post_type' => 'vfic_publications',
'posts_per_page' => '-1',
'tax_query' => $tax_query
)
);
?>
<div id="wrap">
<div id="content" role="main">
<?php if ( $query->have_posts() ) : ?>
<p><h2>Your search returned<?php /* Search Count */ $allsearch = &new WP_Query(array(
//Retreive ALL publications posts
'post_type' => 'vfic_publications',
'posts_per_page' => '-1',
'tax_query' => $tax_query
)); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('result(s)'); wp_reset_query(); ?></h2><br/>
<i>Please contact individual journals to obtain a full copy of the publication.</i><hr /><hr /></p>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'agriflex' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2><br/><hr />
<?php endwhile; // End the loop. Whew. ?>
<?php elseif ( have_posts() ) : ?>
<h1 class="page-title">....<?php printf( __( 'Search %s', 'agriflex' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
<?php
/* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called loop-search.php and that will be used instead.
*/
get_template_part( 'loop', 'search' );
?>
<?php else : ?>
<div id="post-0" class="post no-results not-found">
<h2 class="entry-title"><?php _e( 'Nothing Found', 'agriflex' ); ?></h2>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'agriflex' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div><!-- #post-0 -->
<?php endif; ?>
</div><!-- #content -->
</div><!-- #wrap -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Any help with this is really appreciated.
You need to rename
loop-{custom-post-type}.php
tosingle-{custom-post-type}.php
.And to be explicit, the actual filename, based on your code above, should be:
single-vfic_publications.php
.Get rid of any other template redirects or anything else similar that you’re using. As per the Template Hierarchy, if you have a Custom Post Type named
vfic_publications
, and a template file namedsingle-vfic_publications.php
, WordPress will automatically use it to render the single-post view for that CPT.The other issue that’s going on here is that you appear to be trying to use the single post view page as an archive index page. You don’t need to do that. Instead, create a
archive-vfic_publications.php
file, and WordPress will automatically use it to render the archive-index page view for your CPT.You also appear to be doing tax queries in your template file. You might want to consider instead using taxonomy template files for that purpose.
You use the following code:
But it’s not clear why you would do such a thing. If you’re trying to load loop-vfic_publications.php get_template_part won’t work unless that file is in the theme.
If it is in the theme, then this function is a waste, and you should use the file
single-vfic_publications.php
instead with aget_template_part()
to include the loop file.The
get_template_part
function is used to include a template file, not return the file path, which is expected.I would change your line:
to