mercredi 29 juin 2016

Getting similar posts by title?

I am looking at trying to get all the posts which contain a certain name and the code I have is half working. It pulls through standard posts but not sure how to get it to show all the custom post types as well.

Here is the code in the 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;
}

Here is the code which then uses that function to find posts of that name.

    <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>

My ideal scenario would be to use just the title of the game for example Fallout 4 and it shows all posts containing that in the title.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire