按标题获得类似帖子?

时间:2016-06-29 09:19:22

标签: php mysql wordpress

我正在尝试获取包含特定名称的所有帖子,而我的代码是半工作。它通过标准帖子,但不知道如何让它显示所有自定义帖子类型。

这是functions.php

中的代码
add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 );
function wpse18703_posts_where( $where, &$wp_query )
{
    global $wpdb;
    if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $wpse18703_title ) ) . '%\'';
    }
    return $where;
}

这是代码,然后使用该函数查找该名称的帖子。

    <div class="main-content large-8 medium-8 small-12 columns">
        <h2><?php the_title(); ?> News and Features</h2>
        <?php
        $title = $wp_query->post->post_title;
        $query = new WP_Query(
            array(
            'wpse18703_title' => "$title",
            'posts_per_page' => 10,
             'post__not_in'  => array($post->ID),
                'post_type' => array('post', 'it_review')
            )
        );

        if ($query->have_posts()){
            while ( $query->have_posts() ) { $query->the_post(); ?>
                <div class="small-12 large-12 gdb-news-container">
                <div class="small-12 large-4 columns gdb-news-image">
                    <?php the_post_thumbnail(); ?>
                </div>
                <div class="small-12 large-8 columns">
                    <h3><?php the_title(); ?></h3>
                <?php echo content(20); ?>
                </div>
                    <div class="clearfix"></div>
                </div>

我的理想情况是仅使用游戏的标题,例如Fallout 4,它会显示标题中包含该帖子的所有帖子。

1 个答案:

答案 0 :(得分:0)

用一双新眼睛回顾我的代码,我注意到我没有在'post_type&#39;中添加正确的帖子类型。 =&GT;阵列(&#39;&#39)。

在包含正确的帖子类型之后,它也开始接触这些帖子。

我想我会回答这个,以防其他人像我一样想念它。

相关问题