filter to replace collection link. pre_get_posts for term archives
This commit is contained in:
parent
fb33800ff1
commit
5886e6b939
|
@ -36,7 +36,14 @@ class Taxonomy extends Entity {
|
|||
* @var string
|
||||
*/
|
||||
protected $repository = 'Taxonomies';
|
||||
|
||||
|
||||
/**
|
||||
* Prefix used to create the db_identifier
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
static $db_identifier_prefix = 'tnc_tax_';
|
||||
|
||||
public function __toString(){
|
||||
return 'Hello, my name is '. $this->get_name();
|
||||
}
|
||||
|
@ -138,7 +145,7 @@ class Taxonomy extends Entity {
|
|||
* @return bool|string
|
||||
*/
|
||||
function get_db_identifier() {
|
||||
return $this->get_id() ? 'tnc_tax_' . $this->get_id() : false;
|
||||
return $this->get_id() ? self::$db_identifier_prefix . $this->get_id() : false;
|
||||
}
|
||||
|
||||
// Setters
|
||||
|
|
|
@ -341,12 +341,12 @@ abstract class Repository {
|
|||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public static function get_collections_db_identifier() {
|
||||
public static function get_collections_db_identifiers() {
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
$collections = $Tainacan_Collections->fetch( [], 'OBJECT' );
|
||||
$cpts = [];
|
||||
foreach ( $collections as $col ) {
|
||||
$cpts[ $col->get_db_identifier() ] = $col;
|
||||
$cpts[] = $col->get_db_identifier();
|
||||
}
|
||||
|
||||
return $cpts;
|
||||
|
@ -387,8 +387,8 @@ abstract class Repository {
|
|||
|
||||
// Is it a collection Item?
|
||||
if ( $prefix == Entities\Collection::$db_identifier_prefix ) {
|
||||
$cpts = self::get_collections_db_identifier();
|
||||
if ( array_key_exists( $post_type, $cpts ) ) {
|
||||
$cpts = self::get_collections_db_identifiers();
|
||||
if ( in_array( $post_type, $cpts ) ) {
|
||||
return $entity = new \Tainacan\Entities\Item( $post );
|
||||
} else {
|
||||
throw new \Exception( 'Collection object not found for this post' );
|
||||
|
|
|
@ -21,6 +21,14 @@ class Theme_Helper {
|
|||
add_filter( 'the_content', [&$this, 'the_content_filter'] );
|
||||
|
||||
|
||||
/**
|
||||
* Replace collection single template with the respective post type archive
|
||||
* TODO: if collections is not set to use a cover page
|
||||
*/
|
||||
add_filter('post_type_link', array(&$this, 'permalink_filter'), 10, 3);
|
||||
|
||||
|
||||
add_action('pre_get_posts', array(&$this, 'tax_archive_pre_get_posts'));
|
||||
|
||||
}
|
||||
|
||||
|
@ -30,6 +38,15 @@ class Theme_Helper {
|
|||
return $prefix == Entities\Collection::$db_identifier_prefix;
|
||||
}
|
||||
|
||||
public function is_taxonomy_a_tainacan_tax($tax_slug) {
|
||||
$prefix = substr( $tax_slug, 0, strlen( Entities\Taxonomy::$db_identifier_prefix ) );
|
||||
return $prefix == Entities\Taxonomy::$db_identifier_prefix;
|
||||
}
|
||||
|
||||
public function is_term_a_tainacan_term( \WP_Term $term ) {
|
||||
return $this->is_taxonomy_a_tainacan_tax($term->taxonomy);
|
||||
}
|
||||
|
||||
public function the_content_filter($content) {
|
||||
global $post;
|
||||
|
||||
|
@ -55,6 +72,49 @@ class Theme_Helper {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the permalink for posts to:
|
||||
*
|
||||
* * Replace Collectino single permalink with the link to the post type archive for items of that collection
|
||||
*
|
||||
* @return string new permalink
|
||||
*/
|
||||
function permalink_filter($permalink, $post, $leavename) {
|
||||
|
||||
$collection_post_type = \Tainacan\Entities\Collection::get_post_type();
|
||||
|
||||
if (!is_admin() && $post->post_type == $collection_post_type) {
|
||||
|
||||
$collection = new \Tainacan\Entities\Collection($post);
|
||||
$items_post_type = $collection->get_db_identifier();
|
||||
|
||||
$post_type_object = get_post_type_object($items_post_type);
|
||||
|
||||
if (isset($post_type_object->rewrite) && is_array($post_type_object->rewrite) && isset($post_type_object->rewrite['slug']))
|
||||
return site_url($post_type_object->rewrite['slug']);
|
||||
|
||||
}
|
||||
|
||||
return $permalink;
|
||||
|
||||
}
|
||||
|
||||
function tax_archive_pre_get_posts($wp_query) {
|
||||
|
||||
if (!is_tax() || !$wp_query->is_main_query())
|
||||
return $wp_query;
|
||||
|
||||
$term = get_queried_object();
|
||||
|
||||
if ($this->is_term_a_tainacan_term($term)) {
|
||||
// TODO: Why post_type = any does not work?
|
||||
$wp_query->set( 'post_type', \Tainacan\Repositories\Repository::get_collections_db_identifiers() );
|
||||
}
|
||||
|
||||
return $wp_query;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue