php - Getting similar posts by title? -
i looking @ trying posts contain name , code have half working. pulls through standard posts not sure how show custom post types well.
here code in 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 .= ' , ' . $wpdb->posts . '.post_title \'' . esc_sql( $wpdb->esc_like( $wpse18703_title ) ) . '%\''; } return $where; }
here code uses function find posts of name.
<div class="main-content large-8 medium-8 small-12 columns"> <h2><?php the_title(); ?> news , 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>
my ideal scenario use title of game example fallout 4 , shows posts containing in title.
looking on code fresh pair of eyes noticed did not put correct post type in 'post_type' => array('').
after including correct post type has started pull through posts well.
thought answer in case else miss me.
Comments
Post a Comment