将mailster插件中功能get_last_post的顺序更改为自定义字段顺序

时间:2019-04-07 10:36:04

标签: php wordpress

我想将post 的帖子的 order 从默认的wordpress顺序post_date更改为mailster插件所使用的,将其更改为ADVANCED CUSTOM FIELDS创建的自定义字段

我使用最新版本的mailster和wordpress 5

这是控制插件内发布顺序的代码源

 public function get_last_post( $offset = 0, $post_type = 'post', $term_ids = array(), $args = array(), $campaign_id = null, $subscriber_id = null ) {

        global $wpdb;

        $args = apply_filters( 'mailster_pre_get_last_post_args', $args, $offset, $post_type, $term_ids, $campaign_id, $subscriber_id );

        $key = md5( serialize( array( $offset, $post_type, $term_ids, $args, $campaign_id, $subscriber_id ) ) );

        $posts = mailster_cache_get( 'get_last_post' );

        if ( $posts && isset( $posts[ $key ] ) ) {
            return $posts[ $key ];
        }

        $post = apply_filters( 'mailster_get_last_post_' . $post_type, null, $args, $offset, $term_ids, $campaign_id, $subscriber_id );

        if ( is_null( $post ) ) {

            $defaults = array(
                'posts_per_page' => 1,
                'numberposts' => 1,
                'post_type' => $post_type,
                'offset' => $offset,
                'update_post_meta_cache' => false,
                'no_found_rows' => true,
                'cache_results' => false,
            );

            if ( ! isset( $args['post__not_in'] ) ) {
                $exclude = mailster_cache_get( 'get_last_post_ignore' );

                if ( ! $exclude ) {
                    $exclude = $wpdb->get_col( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'mailster_ignore' AND meta_value != '0'" );
                }

                if ( ! empty( $exclude ) ) {
                    $args['post__not_in'] = (array) $exclude;
                }
            }
            $args = wp_parse_args( $args, $defaults );

            mailster_cache_set( 'get_last_post_ignore', $exclude );

            if ( ! empty( $term_ids ) ) {

                $tax_query = array();

                $taxonomies = get_object_taxonomies( $post_type, 'names' );

                for ( $i = 0; $i < count( $term_ids ); $i++ ) {
                    if ( empty( $term_ids[ $i ] ) ) {
                        continue;
                    }

                    $tax_query[] = array(
                        'taxonomy' => $taxonomies[ $i ],
                        'field' => 'id',
                        'terms' => explode( ',', $term_ids[ $i ] ),
                    );
                }

                if ( ! empty( $tax_query ) ) {
                    $tax_query['relation'] = 'AND';
                    $args = wp_parse_args( $args, array( 'tax_query' => $tax_query ) );
                }
            }

            $args = apply_filters( 'mailster_get_last_post_args', $args, $offset, $post_type, $term_ids, $campaign_id, $subscriber_id );

            $posts = get_posts( $args );

        } else {
            $posts = array( $post );
        }

        if ( $posts ) {
            $post = $posts[0];

            if ( ! $post->post_excerpt ) {
                if ( preg_match( '/<!--more(.*?)?-->/', $post->post_content, $matches ) ) {
                    $content = explode( $matches[0], $post->post_content, 2 );
                    $post->post_excerpt = trim( $content[0] );
                }
            }

            $post->post_excerpt = mailster( 'helper' )->get_excerpt( ( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content), apply_filters( 'mailster_excerpt_length', null ) );

            $post->post_excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );

            $post->post_content = apply_filters( 'the_content', $post->post_content );

        } else {
            $post = false;
        }

        if ( ! isset( $args['cache_results'] ) || $args['cache_results'] !== false ) {
            $posts[ $key ] = $post;

            mailster_cache_set( 'get_last_post', $posts );
        }

        return $post;
    }

我尝试这样做,但是我不知道何时可以放置此代码以及如何创建自定义函数来更改代码

 $args = array(
      'post_type' => 'post',
      'order' => 'ASC',
      'meta_key'=>'post_position'
      'orderby'=> 'meta_value',

  );

0 个答案:

没有答案