Related post using post title

时间:2013-12-22 作者:lucas

是否可以根据帖子标题而不是标签和类别获取相关帖子?类似-
第1篇标题:WordPress技巧
第2篇标题:WordPress技巧

在本例中,相关帖子应该在从标题中选取的关键字WordPress上生成,而不是从发布到的已用标记或类别中提取。

下面是我现在在函数中使用的内容。php

add_image_size( \'related\', 100, 100, true );
add_action( \'genesis_after_post_content\', \'child_related_posts\' );
add_action( \'genesis_after_entry_content\', \'child_related_posts\' );

function child_related_posts() {

if ( is_single ( ) ) {

    global $post;

    $count = 0;
    $postIDs = array( $post->ID );
    $related = \'\';
    $tags = wp_get_post_tags( $post->ID );
    $cats = wp_get_post_categories( $post->ID );

    if ( $tags ) {

        foreach ( $tags as $tag ) {

            $tagID[] = $tag->term_id;

        }

        $args = array(
            \'tag__in\'               => $tagID,
            \'post__not_in\'          => $postIDs,
            \'showposts\'             => 5,
            \'ignore_sticky_posts\'   => 1,
            \'tax_query\'             => array(
                array(
                                    \'taxonomy\'  => \'post_format\',
                                    \'field\'     => \'slug\',
                                    \'terms\'     => array( 
                                        \'post-format-link\', 
                                        \'post-format-status\', 
                                        \'post-format-aside\', 
                                        \'post-format-quote\'
                                        ),
                                    \'operator\'  => \'NOT IN\'
                )
            )
        );

        $tag_query = new WP_Query( $args );

        if ( $tag_query->have_posts() ) {

            while ( $tag_query->have_posts() ) {

                $tag_query->the_post();

                $img = genesis_get_image() ? genesis_get_image( array( \'size\' => \'related\' ) ) : \'<img src="\' . get_bloginfo( \'stylesheet_directory\' ) . \'/images/related.png" alt="\' . get_the_title() . \'" />\';

                $related .= \'<li><a href="\' . get_permalink() . \'" rel="bookmark" title="Permanent Link to\' . get_the_title() . \'">\' . $img . get_the_title() . \'</a></li>\';

                $postIDs[] = $post->ID;

                $count++;
            }
        }
    }

    if ( $count <= 4 ) {

        $catIDs = array( );

        foreach ( $cats as $cat ) {

            if ( 3 == $cat )
                continue;
            $catIDs[] = $cat;

        }

        $showposts = 5 - $count;

        $args = array(
            \'category__in\'          => $catIDs,
            \'post__not_in\'          => $postIDs,
            \'showposts\'             => $showposts,
            \'ignore_sticky_posts\'   => 1,
            \'orderby\'               => \'rand\',
            \'tax_query\'             => array(
                                array(
                                    \'taxonomy\'  => \'post_format\',
                                    \'field\'     => \'slug\',
                                    \'terms\'     => array( 
                                        \'post-format-link\', 
                                        \'post-format-status\', 
                                        \'post-format-aside\', 
                                        \'post-format-quote\' ),
                                    \'operator\' => \'NOT IN\'
                                )
            )
        );

        $cat_query = new WP_Query( $args );

        if ( $cat_query->have_posts() ) {

            while ( $cat_query->have_posts() ) {

                $cat_query->the_post();

                $img = genesis_get_image() ? genesis_get_image( array( \'size\' => \'related\' ) ) : \'<img src="\' . get_bloginfo( \'stylesheet_directory\' ) . \'/images/related.png" alt="\' . get_the_title() . \'" />\';

                $related .= \'<li><a href="\' . get_permalink() . \'" rel="bookmark" title="Permanent Link to\' . get_the_title() . \'">\' . $img . get_the_title() . \'</a></li>\';
            }
        }
    }

    if ( $related ) {

        printf( \'<div class="related-posts"><h3 class="related-title">Related Posts</h3><ul class="related-list">%s</ul></div>\', $related );

    }

    wp_reset_query();

}
}
因为我使用的是genesis主题,所以我得到了有限的空间来编辑文件,通常只限于编辑功能。php和样式。子主题中的css。

也尝试了一些其他代码,但这是我在没有插件的情况下最接近的东西。这段代码的问题是,它根据标签而不是帖子的标题来拉取相关帖子。

1 个回复
SO网友:s_ha_dum

一个简单的WP_Query 使用s 参数将搜索帖子标题和帖子内容:

$args = array(
  \'post_type\' => \'post\',
  \'s\' => \'keyword\'
);
$query = new WP_Query($args);
var_dump($query->request);
但您可以使用以下几种过滤器之一进一步限制:

function restrict_search($search,$s) {
  remove_filter(\'posts_search\',\'restrict_search\');
  global $wpdb;
  return $wpdb->prepare(" AND {$wpdb->posts}.post_title LIKE \'%%%s%%\' ",$s->query[\'s\']);
}
add_filter(\'posts_search\',\'restrict_search\',1,2);
$args = array(
  \'post_type\' => \'post\',
  \'s\' => \'keyword\'
);
$query = new WP_Query($args);
var_dump($query->request);

结束

相关推荐

WP_Query in functions.php

我有一些代码要转换成函数。它工作得很好,直到我将其包装到所述函数中: $args = array( \'posts_per_page\' => -1, \'post_type\' => \'asset\', \'category_name\' => $cat ); $cat_query = new WP_Query( $args );