I think I may have something. Following my idea above and keesiemeijer’s point about the comments_template needing a $post object. The methodology is
- Create a new hidden custom post-type to which the comments are bound.
- Provide for basic post instance creation.
- By default the comments for will loop back to the post-types default url and not the referring page. Therefore we need to provide filters for get_comments_link.
- A wp_rewrite rule addition is needed to deal with paged comments
- Minimal wrapper to comments_template
The key is creating some sort of deterministic link between a single term and a single post. The following is a proof of concept.
define( 'CAW_TYPE', 'caw_stubb' );
//ACTIONS
add_action( 'init', 'caw_init', 100);
//FILTERS
add_filter( 'get_comment_link', 'caw_comment_link', 1, 3 );
//INIT Function -- setup post type and wp_rewrite riles
function caw_init() {
$singular = 'Comments Stub';
$plural = 'Comments Stubs';
$args = array(
'label' => $plural,
'labels' => array( 'name' => $plural, 'singular_name' => $singular, 'add_new' => 'Add '.$singular, 'add_new_item' => 'Add New '.$singular, 'edit_item' => 'Edit '.$singular, 'new_item' => 'New '.$singular, 'view_item' => 'View '.$singular, 'search_items' => 'Search '.$plural, 'not_found' => 'No '.$singular.' found', 'not_found_in_trash' => 'No '.$singular.' found in Trash', 'parent_item_colon' => $singular.': ' ),
'description' => 'Hidden post type used to bind any comment to',
'public' => false,
'capability_type' => 'post',
'supports' => array( 'title', 'trackbacks', 'custom-fields', 'comments' ),
'query_var' => false
);
register_post_type( CAW_TYPE, $args );
caw_add_rewrite_rules($rules);
}
//ADD wp_rewrite rules for all public taxonomies
function caw_add_rewrite_rules($rules) {
$taxes = get_taxonomies( array(), 'objects');
foreach ( $taxes as $tax ) {
if ( $tax->public ) {
add_rewrite_rule(
$tax->rewrite['slug'].'/([^/]+)/comment-page-([0-9]{1,})/?$',
'index.php?'.$tax->query_var.'=$matches[1]&cpage=$matches[2]',
'top');
}
}
flush_rewrite_rules();
}
//FILTER get_comments_link to redirect from the hidden post types uri to the uri
//uri of the dynamically generated term page.
function caw_comment_link($link,$comment,$var) {
global $post;
if ( get_post_type() == CAW_TYPE ) {
$meta = get_post_custom();
$postlink = get_permalink($comment_post_ID);
$termlink = get_term_link( intval($meta['caw_term_id'][0]), $meta['caw_taxonomy'][0] );
$link = str_replace( $postlink, $termlink, $link );
}
return $link;
}
//RETURN the hidden post associated with a specific term object. Optionally
//create that post if it does not already exist
function caw_getstubb_term($term, $create=true) {
$oldposts = get_posts( array( 'name' => $term->taxonomy.'-'.$term->term_id, 'post_type' => CAW_TYPE ));
if ( count($oldposts) == 1 ) {
return $oldposts[0];
} elseif ( $create) {
$newpostid = caw_addstubb_term($term);
if ( !is_wp_error($newpost) ) {
return get_post($newpost);
} else {
return false;
}
} else {
return false;
}
}
//CREATE hiiden post associated with a specific term object. Record the parent term using
//the post custom fields
function caw_addstubb_term($term) {
$newpost = wp_insert_post(
array(
'post_title' => $term->name,
'post_name' => $term->taxonomy.'-'.$term->term_id,
'post_status' => 'publish',
'post_type' => CAW_TYPE
)
);
add_post_meta($newpost->ID, 'caw_taxonomy', $term->taxonomy, true);
add_post_meta($newpost->ID, 'caw_term_id', $term->term_id, true);
return $newpost;
}
//DISPLAY COMMENTS function. Retrieves hidden post & runs comments_template.
// $file and $seperate are passed directly to comments_template.
function caw_comments_template($term, $file='', $seperate=true, $create=true) {
global $post, $withcomments;
if ( isset($post) ) $oldpost = $post;
$post = caw_getstubb_term($term, $create);
$withcomments = "1";
comments_template( $file, $seperate);
if ( isset($oldpost) ) $post = $oldpost;
}
This is still very generic as it adds the possibility of comments on all public taxonomies. Ideally the creation of the hidden posts would be hooked into the creation/deletion of the original terms. However, I had to do this for a site redesign where those terms were already in
existence.