更新相关帖子后被删除的标签:ACF/Genesis/WP5.4.1

时间:2020-04-09 作者:Jim

我有一个自定义的“news”帖子类型,其中包含“news tags”分类法。以及另一种在新闻项目中标记的帖子类型“实践”。当我更新任何“实践”类型的帖子时,all tags for that practice are being incorrectly removed 来自所有新闻项目。例如,如果我用“不动产”实践标记新闻帖子,然后更新不动产实践,则不动产标记将从所有新闻项目中删除。有人知道我应该从哪里开始故障排除吗?我已经删除了除ACF之外的所有插件,问题仍然存在。

The News Post Type...

<?php

if ( ! defined( \'ABSPATH\' ) )
    exit; 

if( class_exists( \'CT_News\' ) )
    return;

class CT_News {

    const POST_TYPE = \'ct-news\';
    const TAX_TAGS = \'ct-news-tags\';
    const PERMALINK = \'news\';

    public function __construct( $post = NULL ){
        if( is_object( $post ) ){
            foreach ( get_object_vars( $post ) as $key => $value )
                $this->$key = $value;
        }
    }

    public function get_post_type(){
        return self::POST_TYPE;
    }

    public function get_tax_tags(){
        return self::TAX_TAGS;
    }

    public function _init(){
        add_action( \'init\', array( $this, \'_register\' ) );
        add_action( \'init\', array( $this, \'_register_tax\' ) );
        add_filter( \'enter_title_here\', array( $this, \'_title_text\' ) );
        add_shortcode( \'display-news\', array( $this, \'display_news_shortcode\' ) );
        add_action( \'pre_get_posts\', array( $this, \'filter_news_archive\' ) );
        add_filter( \'searchwp_swp_query_args\', array( $this, \'filter_news\' ) );

        // Setup Custom Sidebar
        add_action( \'init\', array( $this, \'setup_sidebars\' ) );
    }

    public function _register(){
        $labels = array(
            \'name\'                  => _x( \'Articles\', \'Post Type General Name\', \'colmt\' ),
            \'singular_name\'         => _x( \'Article\', \'Post Type Singular Name\', \'colmt\' ),
            \'menu_name\'             => __( \'News\', \'colmt\' ),
            \'name_admin_bar\'        => __( \'News\', \'colmt\' ),
            \'archives\'              => __( \'News Archives\', \'colmt\' ),
            \'parent_item_colon\'     => __( \'Parent Article:\', \'colmt\' ),
            \'all_items\'             => __( \'All News\', \'colmt\' ),
            \'add_new_item\'          => __( \'Add New Article\', \'colmt\' ),
            \'add_new\'               => __( \'Add New\', \'colmt\' ),
            \'new_item\'              => __( \'New Article\', \'colmt\' ),
            \'edit_item\'             => __( \'Edit Article\', \'colmt\' ),
            \'update_item\'           => __( \'Update Article\', \'colmt\' ),
            \'view_item\'             => __( \'View Article\', \'colmt\' ),
            \'search_items\'          => __( \'Search News\', \'colmt\' ),
            \'not_found\'             => __( \'Not found\', \'colmt\' ),
            \'not_found_in_trash\'    => __( \'Not found in Trash\', \'colmt\' ),
            \'featured_image\'        => __( \'Featured Image\', \'colmt\' ),
            \'set_featured_image\'    => __( \'Set featured image\', \'colmt\' ),
            \'remove_featured_image\' => __( \'Remove featured image\', \'colmt\' ),
            \'use_featured_image\'    => __( \'Use as featured image\', \'colmt\' ),
            \'insert_into_item\'      => __( \'Insert into article\', \'colmt\' ),
            \'uploaded_to_this_item\' => __( \'Uploaded to this article\', \'colmt\' ),
            \'items_list\'            => __( \'News list\', \'colmt\' ),
            \'items_list_navigation\' => __( \'News list navigation\', \'colmt\' ),
            \'filter_items_list\'     => __( \'Filter news list\', \'colmt\' ),
        );
        $rewrite = array(
            \'slug\'                  => self::PERMALINK,
            \'with_front\'            => false,
            \'pages\'                 => true,
            \'feeds\'                 => true,
        );
        $args = array(
            \'label\'                 => __( \'Article\', \'colmt\' ),
            \'description\'           => __( \'Colmt News Articles\', \'colmt\' ),
            \'labels\'                => $labels,
            \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'revisions\', \'genesis-seo\', \'genesis-cpt-archives-settings\' ),
            \'taxonomies\'            => array( self::TAX_TAGS ),
            \'hierarchical\'          => false,
            \'public\'                => true,
            \'show_ui\'               => true,
            \'show_in_menu\'          => true,
            \'menu_position\'         => 5,
            \'menu_icon\'             => \'dashicons-format-aside\',
            \'show_in_admin_bar\'     => true,
            \'show_in_nav_menus\'     => true,
            \'can_export\'            => true,
            \'has_archive\'           => self::PERMALINK,
            \'exclude_from_search\'   => false,
            \'publicly_queryable\'    => true,
            \'rewrite\'               => $rewrite,
            \'capability_type\'       => \'post\',
        );
        register_post_type( self::POST_TYPE, $args );
    }

    public function _register_tax(){
        $labels = array(
            \'name\'                       => _x( \'Tags\', \'Taxonomy General Name\', \'colmt\' ),
            \'singular_name\'              => _x( \'Tag\', \'Taxonomy Singular Name\', \'colmt\' ),
            \'menu_name\'                  => __( \'News Tags\', \'colmt\' ),
            \'all_items\'                  => __( \'All Tags\', \'colmt\' ),
            \'parent_item\'                => __( \'Parent Tag\', \'colmt\' ),
            \'parent_item_colon\'          => __( \'Parent Tag:\', \'colmt\' ),
            \'new_item_name\'              => __( \'New Tag Name\', \'colmt\' ),
            \'add_new_item\'               => __( \'Add New Tag\', \'colmt\' ),
            \'edit_item\'                  => __( \'Edit Tag\', \'colmt\' ),
            \'update_item\'                => __( \'Update Tag\', \'colmt\' ),
            \'view_item\'                  => __( \'View Tag\', \'colmt\' ),
            \'separate_items_with_commas\' => __( \'Separate tags with commas\', \'colmt\' ),
            \'add_or_remove_items\'        => __( \'Add or remove tags\', \'colmt\' ),
            \'choose_from_most_used\'      => __( \'Choose from the most used\', \'colmt\' ),
            \'popular_items\'              => __( \'Popular Tags\', \'colmt\' ),
            \'search_items\'               => __( \'Search News Tags\', \'colmt\' ),
            \'not_found\'                  => __( \'Not Found\', \'colmt\' ),
            \'no_terms\'                   => __( \'No tags\', \'colmt\' ),
            \'items_list\'                 => __( \'Tags list\', \'colmt\' ),
            \'items_list_navigation\'      => __( \'Tags list navigation\', \'colmt\' ),
        );
        $rewrite = array(
            \'slug\'                       => \'news-tags\',
            \'with_front\'                 => false,
            \'hierarchical\'               => false,
        );
        $args = array(
            \'labels\'                     => $labels,
            \'hierarchical\'               => false,
            \'public\'                     => true,
            \'show_ui\'                    => true,
            \'show_admin_column\'          => true,
            \'show_in_nav_menus\'          => false,
            \'show_tagcloud\'              => true,
            \'rewrite\'                    => $rewrite,
        );
        register_taxonomy( self::TAX_TAGS, array( self::POST_TYPE ), $args );
    }

    function _title_text( $title ){
        $screen = get_current_screen();

        if ( $screen->post_type != self::POST_TYPE )
            return $title;

        return __( \'Enter Article Title\', \'colmt\' );
    }

    public function archive_adjustments(){
        if( ! is_post_type_archive( self::POST_TYPE ) )
            return;

        //* Force Full Width Content Layout
        add_filter( \'genesis_pre_get_option_site_layout\', \'__genesis_return_full_width_content\' );
    }

    public function get_related( $context, $post_id = NULL, $count = 1 ){
        $news_args = array(
            \'post_type\' => self::POST_TYPE,
            \'post_status\' => \'publish\',
            \'posts_per_page\' => $count,
            \'meta_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => $context . \'-tags\',
                    \'value\' => \'"\' . $post_id . \'"\', // wrap in quotes to match exact value in serialized field.
                    \'compare\' => \'LIKE\'
                )
            )
        );

        return new WP_Query( $news_args );
    }

    /**
     * Display News Shortcode
     * @param  array  $atts    Shortcode Atts
     * @param  string $content Shortcode Content
     * @return string          HTML/Content
     */
    function display_news_shortcode( $atts = array(), $content = NULL ){
        $atts = shortcode_atts(
            array(
                \'count\' => 3,
                \'context\' => NULL,
                \'format\' => \'F j, Y\',
                \'more-text\' => __( \'More News\', \'colmt\' ),
                \'more-class\' => \'more-news\',
                \'related\' => NULL
            ), $atts, \'display-news\' );

        if( $atts[\'related\'] && $atts[\'context\'] ){
            $news = $this->get_related( $atts[\'context\'], $atts[\'related\'], $atts[\'count\'] );
        } else {
            $news_args = array(
                \'post_type\' => self::POST_TYPE,
                \'post_status\' => \'publish\',
                \'posts_per_page\' => $atts[\'count\']
            );

            $news = new WP_Query( $news_args );
        }

        if( ! $news->have_posts() )
            return \'\';

        $output = \'<div class="news">\';

        global $post;

        if( count( $news->posts ) < $atts[\'count\'] )
            $atts[\'more-text\'] = false;
        while( $news->have_posts() ): $news->the_post();
            $permalink = get_permalink();
            $excerpt = $post->post_excerpt ? get_the_excerpt() : get_the_title();
            $output .= \'<div class="news-item">\';
                $output .= \'<div class="news-date">\' . get_the_date( $atts[\'format\'] ) . \'</div>\';
                $output .= \'<div class="news-excerpt"><a href="\' . esc_attr( $permalink ) . \'">\';
                    $output .= $excerpt;
                $output .= \'</a></div>\';
            $output .= \'</div><!-- .news-item -->\';
        endwhile;
        wp_reset_postdata();

        if( $atts[\'more-text\'] ){
            $more_link = \'/\' . self::PERMALINK . \'/\';
            if( $atts[\'related\'] && $atts[\'context\'] )
                $more_link .= \'?\' . $atts[\'context\'] . \'=\' . $atts[\'related\'];
            $output .= \'<p class="read-more-news"><a href="\' . $more_link . \'" class="\' . esc_attr( $atts[\'more-class\'] ) . \'">\' . $atts[\'more-text\'] . \'</a></p>\';
        }

        $output .= \'</div><!-- .news -->\';

        return $output;
    }

    public function setup_sidebars(){
        /**
         * News Article (Singular) Sidebar
         */
        genesis_register_sidebar( array(
            \'id\'            => \'news-article\',
            \'name\'          => __( \'News Article (Singular)\', \'colmt\' ),
            \'description\'   => __( \'This is displayed on singular News Article templates\', \'colmt\' ),
        ) );
    }

    function filter_news_archive( $q ){
        if( ! $q->is_main_query() )
            return;

        if( ! $q->is_post_type_archive( self::POST_TYPE ) )
            return;

        $args = $this->filter_news();
        $meta_query = $q->get(\'meta_query\');
        if( ! is_array( $meta_query ) )
            $meta_query = array();

        unset( $args[\'meta_query\'][\'relation\'] );

        foreach( $args[\'meta_query\'] as $mq ){
            $meta_query[] = $mq;
        }

        $meta_query[\'relation\'] = \'OR\';

        $q->set( \'meta_query\', $meta_query );
    }

    public function filter_news( $args = array() ){

        if( ! is_post_type_archive( self::POST_TYPE ) )
            return $args;

        $meta_query = isset( $args[\'meta_query\'] ) ? $args[\'meta_query\'] : array();

        if( ! is_array( $meta_query ) )
            $meta_query = array();

        // apply filters
        if( isset( $_GET[\'practice-areas\'] ) ){
            $practice_area_id = intval( $_GET[\'practice-areas\'] );

            $meta_query[] = array(
                \'key\' => \'practice-areas-tags\',
                \'value\' => \'"\' . $practice_area_id . \'"\',
                \'compare\' => \'LIKE\'
            );
        }

        if( isset( $_GET[\'attorneys\'] ) ){
            $attorney_id = intval( $_GET[\'attorneys\'] );

            $meta_query[] = array(
                \'key\' => \'attorneys-tags\',
                \'value\' => \'"\' . $attorney_id . \'"\',
                \'compare\' => \'LIKE\'
            );
        }

        $args[\'meta_query\'][\'relation\'] = \'OR\';
        $args[\'meta_query\'] = $meta_query;

        return $args;
    }

}

The Practices Post Type:

<?php

if ( ! defined( \'ABSPATH\' ) )
    exit;

if( class_exists( \'CT_Practice_Area\' ) )
    return;

class CT_Practice_Area {

    const POST_TYPE = \'ct-practice-area\';
    private static $_this;

    public function __construct( $post = NULL ){
        self::$_this = $this;

        if( is_object( $post ) ){
            foreach ( get_object_vars( $post ) as $key => $value )
                $this->$key = $value;
        }
    }

    public function get_post_type(){
        return self::POST_TYPE;
    }

    public function _init(){
        add_action( \'init\', array( $this, \'_register\' ) );
        add_filter( \'enter_title_here\', array( $this, \'_title_text\' ) );
        add_shortcode( \'practice-areas-list\', array( $this, \'practice_areas_list_shortcode\' ) );

        // bidirectional field syncs
        add_filter( \'acf/update_value/name=clients-tags\', array( self::$_this, \'bidirectional_sync_practice_areas_tags\' ), 10, 3 );
        add_filter( \'acf/update_value/name=practice-area-contacts\', array( self::$_this, \'bidirectional_sync_attorney_practice_area_contacts\' ), 10, 3 );
        add_filter( \'acf/update_value/name=practice-area-attorneys\', array( self::$_this, \'bidirectional_sync_attorney_practice_areas\' ), 10, 3 );

        //add_action( \'template_redirect\', array( $this, \'disable_root_practice_areas\' ) );

        add_action( \'init\', array( $this, \'setup_sidebars\' ) );

        add_filter( \'pre_get_posts\', array( $this, \'archive_query_adjustments\' ) );

        add_filter( \'get_previous_post_where\', array( $this, \'exclude_parent_posts\' ), 10, 5 );
        add_filter( \'get_next_post_where\', array( $this, \'exclude_parent_posts\' ), 10, 5 );

        add_image_size( \'practice-area-archive\', 220, 110, true );
    }

    public function _register(){
        $labels = array(
            \'name\'                  => _x( \'Services\', \'Post Type General Name\', \'colmt\' ),
            \'singular_name\'         => _x( \'Service\', \'Post Type Singular Name\', \'colmt\' ),
            \'menu_name\'             => __( \'Services\', \'colmt\' ),
            \'name_admin_bar\'        => __( \'Services\', \'colmt\' ),
            \'archives\'              => __( \'Services\', \'colmt\' ),
            \'parent_item_colon\'     => __( \'Parent Service:\', \'colmt\' ),
            \'all_items\'             => __( \'All Services\', \'colmt\' ),
            \'add_new_item\'          => __( \'Add New Service\', \'colmt\' ),
            \'add_new\'               => __( \'Add New\', \'colmt\' ),
            \'new_item\'              => __( \'New Service\', \'colmt\' ),
            \'edit_item\'             => __( \'Edit Service\', \'colmt\' ),
            \'update_item\'           => __( \'Update Service\', \'colmt\' ),
            \'view_item\'             => __( \'View Service\', \'colmt\' ),
            \'search_items\'          => __( \'Search Services\', \'colmt\' ),
            \'not_found\'             => __( \'Not found\', \'colmt\' ),
            \'not_found_in_trash\'    => __( \'Not found in Trash\', \'colmt\' ),
            \'featured_image\'        => __( \'Featured Image\', \'colmt\' ),
            \'set_featured_image\'    => __( \'Set featured image\', \'colmt\' ),
            \'remove_featured_image\' => __( \'Remove featured image\', \'colmt\' ),
            \'use_featured_image\'    => __( \'Use as featured image\', \'colmt\' ),
            \'insert_into_item\'      => __( \'Insert into Service Description\', \'colmt\' ),
            \'uploaded_to_this_item\' => __( \'Uploaded to this Service\', \'colmt\' ),
            \'items_list\'            => __( \'Services list\', \'colmt\' ),
            \'items_list_navigation\' => __( \'Services list navigation\', \'colmt\' ),
            \'filter_items_list\'     => __( \'Filter Services list\', \'colmt\' ),
        );
        $rewrite = array(
            \'slug\'                  => \'practice-area\',
            \'with_front\'            => false,
            \'pages\'                 => true,
            \'feeds\'                 => true,
        );
        $args = array(
            \'label\'                 => __( \'Practice Area\', \'colmt\' ),
            \'description\'           => __( \'Colmt Practice Areas\', \'colmt\' ),
            \'labels\'                => $labels,
            \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\', \'revisions\', \'page-attributes\', \'genesis-seo\' ),
            \'hierarchical\'          => true,
            \'public\'                => true,
            \'show_ui\'               => true,
            \'show_in_menu\'          => true,
            \'menu_position\'         => 20,
            \'menu_icon\'             => \'dashicons-index-card\',
            \'show_in_admin_bar\'     => true,
            \'show_in_nav_menus\'     => true,
            \'can_export\'            => true,
            \'has_archive\'           => \'practice-areas\',
            \'exclude_from_search\'   => false,
            \'publicly_queryable\'    => true,
            \'rewrite\'               => $rewrite,
            \'capability_type\'       => \'post\',
        );
        register_post_type( self::POST_TYPE, $args );
    }

    // function disable_root_practice_areas(){
    //  if( ! is_singular( self::POST_TYPE ) )
    //      return;

    //  $obj = get_queried_object();
    //  if( ! $obj->post_parent ){
    //      wp_redirect( \'/practice-areas/\' );
    //      exit();
    //  }
    // }

    function _title_text( $title ){
        $screen = get_current_screen();

        if ( $screen->post_type != self::POST_TYPE )
            return $title;

        return __( \'Enter Practice Area Name\', \'colmt\' );
    }

    public function bidirectional_sync_practice_areas_tags( $value, $post_id, $field ) {
        return ct_sync_field( \'practice-areas-tags\', $value, $post_id, $field );
    }

    public function bidirectional_sync_attorney_practice_area_contacts( $value, $post_id, $field ) {
        return ct_sync_field( \'attorney-practice-area-contacts\', $value, $post_id, $field );
    }

    public function bidirectional_sync_attorney_practice_areas( $value, $post_id, $field ) {
        return ct_sync_field( \'attorney-practice-areas\', $value, $post_id, $field );
    }

    public function setup_sidebars(){

        genesis_register_sidebar( array(
            \'id\'            => \'practice-area\',
            \'name\'          => __( \'Practice Area (Singular)\', \'colmt\' ),
            \'description\'   => __( \'This is displayed on singular Practice Area templates\', \'colmt\' ),
        ) );
    }

    public function archive_query_adjustments( $q ){
        if( is_admin() )
            return $q;

        if( ! $q->is_main_query() || ! $q->is_post_type_archive( self::POST_TYPE ) )
            return $q;

        $q->set( \'posts_per_page\', \'-1\' );
        $q->set( \'post_parent\', \'0\' );
        $q->set( \'orderby\', \'menu_order title\' );
        $q->set( \'order\', \'ASC\' );
    }

    public function get_children( $parent = NULL ){
        if( ! $parent )
            $parent = get_the_ID();

        $children_args = array(
            \'post_type\' => self::POST_TYPE,
            \'post_parent\' => $parent,
            \'post_status\' => \'publish\',
            \'posts_per_page\' => \'-1\',
            \'orderby\' => \'menu_order post_title\',
            \'order\' => \'ASC\'
        );
        $children = new WP_Query( $children_args );

        return $children;
    }

    public function exclude_parent_posts( $where, $in_same_term, $excluded_terms, $taxonomy, $post ){
        if( get_post_type( $post ) != self::POST_TYPE )
            return $where;

        global $wpdb;
        $exclude_parents = \'p.post_parent != 0\';
        $where .= $where ? \' AND \' . $exclude_parents : $exclude_parents;
        return $where;
    }

    public function practice_areas_list_shortcode( $atts = array(), $content = NULL ){
        $atts = shortcode_atts(
            array(
                \'source\' => NULL,
                \'link-to-practice-area\' => true,
                \'attorney\' => get_the_ID()
            ), $atts, \'practice-areas-list\' );

        ct_boolean_atts( $atts );

        if( ! $atts[\'source\'] )
            $atts[\'source\'] = \'all\';

        $output = \'\';
        $related_practice_areas = false;
        $practice_areas_args = array();

        if( $atts[\'source\'] == \'attorney\' ){
            $related_practice_areas = get_field( \'attorney-practice-areas\', $atts[\'attorney\'] );
            if( ! $related_practice_areas )
                return $output;
        } elseif( $atts[\'source\'] == \'tagged\' ){
            $related_practice_areas = get_field( \'practice-areas-tags\' );
            if( ! $related_practice_areas )
                return $output;
        }

        if( $related_practice_areas ){
            $practice_area_ids = wp_list_pluck( $related_practice_areas, \'ID\' );
            $practice_areas_args[\'post__in\'] = $practice_area_ids;
            $practice_areas_args[\'orderby\'] = \'post__in\';
        }

        if( $atts[\'source\'] == \'all\' ){
            $practice_areas_args = array(
                \'post_status\' => \'publish\',
                \'post_type\' => self::POST_TYPE,
                \'posts_per_page\' => \'-1\',
                \'post_parent\' => 0,
                \'orderby\' => \'menu_order post_title\',
                \'order\' => \'asc\'
            );

            $practice_areas = new WP_Query( $practice_areas_args );
        } else {
            $practice_areas = $this->get_practice_areas( $practice_areas_args );
        }

        if( $practice_areas->have_posts() ):
            $output .= \'<ul class="practice-areas-list">\';
                while( $practice_areas->have_posts() ): $practice_areas->the_post();
                    $title = ct_get_practice_area_title_with_icon( \'%s %s\' );

                    $line = $atts[\'link-to-practice-area\'] && $atts[\'source\'] != \'all\' ? sprintf( \'<a href="%s">%s</a>\', get_permalink( get_the_ID() ), $title ) : sprintf( \'<span>%s</span>\', $title );

                    $output .= sprintf( \'<li class="practice-area">%s\', $line );

                    $children = $this->get_children( get_the_ID() );

                    if( $children->have_posts() && $atts[\'source\'] == \'all\' ):
                        $output .= \'<ul class="child-practice-areas">\';

                        while( $children->have_posts() ): $children->the_post();
                            $line = $atts[\'link-to-practice-area\'] ? sprintf( \'<a href="%s">%s</a>\', get_permalink( get_the_ID() ), get_the_title() ) : sprintf( \'<span>%s</span>\', get_the_title() );
                            $output .= sprintf( \'<li class="practice-area">%s</li>\', $line );

                        endwhile;
                        wp_reset_postdata();
                        $output .= \'</ul><!-- .child-practice-areas -->\';
                    endif;

                    $output .= \'</li>\';
                endwhile;
                wp_reset_postdata();
            $output .= \'</ul><!-- .practice-areas-list -->\';

        endif;

        return $output;
    }

    public function get_practice_areas( $args = array() ){
        $practice_areas_args = array(
            \'post_status\' => \'publish\',
            \'post_type\' => self::POST_TYPE,
            \'posts_per_page\' => \'-1\',
        );

        $practice_areas_args = array_merge( $practice_areas_args, $args );

        return new WP_Query( $practice_areas_args );
    }
}

Some filtering for news searches:

<?php

// add_filter( \'genesis_cpt_archive_intro_text_output\', \'ct_insert_news_search\' );


remove_action( \'genesis_before_loop\', \'genesis_archive_title_description\', 15 );
remove_action( \'genesis_before_loop\', \'genesis_do_taxonomy_title_description\', 15 );
add_action( \'genesis_before_loop\', \'do_taxonomy_title_description\', 16 );
add_filter( \'genesis_term_intro_text_output\', \'ct_insert_news_search\' );

add_filter( \'genesis_pre_get_option_site_layout\', \'__genesis_return_full_width_content\' );


function do_taxonomy_title_description() {

    global $wp_query, $wp_embed; 

    if ( ! is_category() && ! is_tag() && ! is_tax() ) {
        return;
    }

    $term = is_tax() ? get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) ) : $wp_query->get_queried_object();

    if ( ! $term ) {
        return;
    }

    $heading = get_term_meta( $term->term_id, \'headline\', true );
    if ( empty( $heading ) ) {
        $heading = $term->name . " News";
    }

    $intro_text = get_term_meta( $term->term_id, \'intro_text\', true );
    $intro_text = $wp_embed->autoembed( $intro_text );
    $intro_text = do_shortcode( $intro_text );
    $intro_text = wpautop( $intro_text );

    $intro_text = apply_filters( \'genesis_term_intro_text_output\', $intro_text ? $intro_text : \'Filter news by keyword\' );
    // $intro_text = apply_filters( \'genesis_term_intro_text_output\', $intro_text ? $intro_text : \'\' );

    /**
     * Fires at end of doing taxonomy archive title and description.
     *
     * Allows you to reorganize output of the archive headings.
     *
     * @since 2.5.0
     *
     * @param string $heading    Archive heading.
     * @param string $intro_text Archive intro text.
     * @param string $context    Context.
     */
    do_action( \'genesis_archive_title_descriptions\', $heading, $intro_text, \'taxonomy-archive-description\' );

}


function ct_insert_news_search( $text ) {
    $post_type = CT()->news->get_post_type();
    $post_url = get_post_type_archive_link( $post_type );
    $clear_text_url = remove_query_arg( $post_type . \'-filter\', $post_url );

    $filters = \'\';

    if ( isset( $_GET[\'practice-areas\'] ) ){
        $filters .= \'<input type="hidden" name="practice-areas" value="\' . esc_attr( intval( $_GET[\'practice-areas\'] ) ) . \'" />\';
        $clear_text_url = add_query_arg( array( \'practice-areas\' => intval( $_GET[\'practice-areas\'] ) ), $clear_text_url );
    }
    if ( isset( $_GET[\'attorneys\'] ) ){
        $filters .= \'<input type="hidden" name="attorneys" value="\' . esc_attr( intval( $_GET[\'attorneys\'] ) ) . \'" />\';
        $clear_text_url = add_query_arg( array( \'attorneys\' => intval( $_GET[\'attorneys\'] ) ), $clear_text_url );
    }

    $search = \'<form role="search" method="get" class="search-form" action="\' . $post_url . \'">
        <label>
            <a href="\' . $clear_text_url . \'" class="clear-search"><i class="fa fa-times"></i></a>
            <span class="screen-reader-text">\' . strip_tags( $text ) . \'</span>
            <input type="search" class="search-field" value="\' . esc_attr( ct_get_news_search_string() ) . \'" name="\' . $post_type . \'-filter" title="\' . esc_attr( strip_tags( $text ) ) . \'">
            \' . $filters . \'
        </label>
        <input type="submit" class="search-submit" value="Search">
    </form>\';

    $output = \'<div class="news-search">\' . $search . \'</div>\';

    return $output;
}


add_action( \'genesis_entry_header\', \'ct_archive_featured_image\', 2 );

function ct_archive_featured_image(){
    $featured = ct_get_featured_image( \'archive-featured\' );
    if ( ! $featured )
        return;

    echo \'<div class="featured-image">\';
        echo \'<img src="\' . $featured[0] . \'" />\';
    echo \'</div>\';
}

remove_action( \'genesis_entry_header\', \'genesis_post_info\', 12 );
add_action( \'genesis_entry_header\', \'genesis_post_info\', 9 );
add_filter( \'genesis_post_info\', \'ct_news_post_info\' );

function ct_news_post_info( $post_info ) {
    $post_info = \'[post_date] [post_edit]\';
    return $post_info;
}

add_filter( \'genesis_post_meta\', \'ct_news_post_meta\' );


function ct_news_post_meta( $post_meta ) {
    $tags_list = get_the_term_list( get_the_ID(), CT()->news->get_tax_tags(), \'\', \', \' );
    $attorneys = get_field( \'attorneys-tags\' );
    $practice_areas = get_field( \'practice-areas-tags\' );



    $attorneys_list = array();
    $practice_areas_list = array();

    if ( is_array( $attorneys ) ){
        foreach( $attorneys as $attorney ){
            if ( ! $attorney )
                continue;
            $attorneys_list[] = \'<a href="\' . get_permalink( $attorney->ID ) . \'">\' . get_the_title( $attorney->ID ) . \'</a>\';
        }
    }

    if ( is_array( $practice_areas ) ){
        foreach( $practice_areas as $practice_area ){
            if ( ! $practice_area )
                //todo Jim
                echo($practice_area->ID);
                continue;
            $practice_areas_list[] = \'<a href="\' . get_permalink( $practice_area->ID ) . \'">\' . get_the_title( $practice_area->ID ) . \'</a>\';
        }
    }

    $post_meta = $tags_list;
    $post_meta .= $post_meta ? rtrim( \', \' . implode( \', \', $attorneys_list ), \', \' ) : implode( \', \', $attorneys_list );
    $post_meta .= $post_meta ? rtrim( \', \' . implode( \', \', $practice_areas_list ), \', \' ) : implode( \', \', $practice_areas_list );

    //echo($post_meta);
    return $post_meta;
}

add_action( \'genesis_before_loop\', \'ct_news_archive_header\', 17 );

function ct_news_archive_header(){

    $label = ct_get_news_search_string() ? __( \'News Search Results\', \'colmt\' ) : __( \'Most Recent News\', \'colmt\' );


    if ( isset( $_GET[\'attorneys\'] ) ){
        //$attorney_name = get_field( \'attorney-goes-by\', intval( $_GET[\'attorneys\'] ) );
        $attorney_name = get_the_title( intval( $_GET[\'attorneys\'] ) );
        $clear_filter_url = remove_query_arg( \'attorneys\' );
        $label .= \' <span class="filtered-by"><em>\' . __( \'about \', \'colmt\' ) . \'</em><strong>\' . $attorney_name . \'</strong><a href="\' . esc_attr( $clear_filter_url ) . \'"><i class="fa fa-times"></i></a></span>\';
    }


        if ( isset( $_GET[\'practice_areas\'] ) ){
        $practice_area = get_the_title( intval( $_GET[\'practice_areas\'] ) );
        $clear_filter_url = remove_query_arg( \'practice_areas\' );
        $label .= \' <span class="filtered-by"><em>\' . __( \'about \', \'colmt\' ) . \'</em><strong>\' . $practice_area . \'</strong><a href="\' . esc_attr( $clear_filter_url ) . \'"><i class="fa fa-times"></i></a></span>\';
    } else { $label .= \'\'; }


    echo \'<div class="news-header">\';
        echo sprintf( \'<div class="subsection-heading news-heading">%s</div>\', $label );
        echo sprintf( \'<div class="subsection-heading tags-heading">%s</div>\', __( \'Tags\', \'colmt\' ) );
    echo \'</div>\';
}

function ct_get_news_search_string(){
    return isset( $_REQUEST[ CT()->news->get_post_type() . \'-filter\' ] ) ? sanitize_text_field( $_REQUEST[ CT()->news->get_post_type() . \'-filter\' ] ) : \'\';
}

remove_all_actions( \'genesis_before_entry_content\' );
remove_all_actions( \'genesis_entry_content\' );
remove_all_actions( \'genesis_after_entry_content\' );

if ( ct_get_news_search_string() && class_exists( \'SWP_Query\' ) ){
    remove_action( \'genesis_loop\', \'genesis_do_loop\' );
    add_action( \'genesis_loop\', \'ct_news_archive_search\' );
    remove_action( \'genesis_after_endwhile\', \'genesis_posts_nav\' );
}

function ct_news_archive_search(){

    // retrieve our search query if applicable
    $query = ct_get_news_search_string();

    // retrieve our pagination if applicable
    $current_page = isset( $_REQUEST[ CT()->news->get_post_type() . \'-page\'] ) ? absint( $_REQUEST[ CT()->news->get_post_type() . \'-page\'] ) : 1;

    $engine = \'news\'; // taken from the SearchWP settings screen

    // see all args at https://searchwp.com/docs/swp_query/
    $news_query = new SWP_Query( array(
        \'s\'      => $query,
        \'engine\' => $engine,
        \'page\'   => $current_page,
    ) );

    // set up pagination
    $pagination = paginate_links( array(
        \'format\'  => \'?\' . CT()->news->get_post_type() . \'-page=%#%\',
        \'current\' => $current_page,
        \'total\'   => $news_query->max_num_pages,
    ) );


    if ( ! empty( $news_query->posts ) ):

        global $post;

        do_action( \'genesis_before_while\' );
        foreach ( $news_query->posts as $post ):

            setup_postdata( $post );

            do_action( \'genesis_before_entry\' );

            printf( \'<article %s>\', genesis_attr( \'entry\' ) );

                do_action( \'genesis_entry_header\' );

                do_action( \'genesis_entry_footer\' );

            echo \'</article>\';

            do_action( \'genesis_after_entry\' );

        endforeach; //* end of one post
        wp_reset_postdata();

        do_action( \'genesis_after_endwhile\' );

        if ( $news_query->max_num_pages > 1 ): ?>
            <div class="navigation pagination" role="navigation">
                <h2 class="screen-reader-text">Posts navigation</h2>
                <div class="nav-links">
                    <?php echo wp_kses_post( $pagination ); ?>
                </div>
            </div>
        <?php endif;

    else :
        do_action( \'genesis_loop_else\' );
    endif; 

genesis();


1 个回复
SO网友:Jim

这是由于两个ACF场组具有相同的场段塞。进行了一些挖掘。

相关推荐

getting posts by tags

我使用下面的代码通过单词标签获取帖子$search_songs = array( \'posts_per_page\' => -1, \'post_type\' => \'songs\', \'tax_query\' => array ( array ( \'taxonomy\' => \'post_tag\',