WP_QUERY Get posts by category and similar name (Like)

I have a small problem with WP_Query. I want to get the posts filtered by category and with similar project name (like query), so I’m trying this code:

$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'projects',
'name__like' => 'Proj');

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
    echo '<div class="Entradas">'.get_the_title().'</div>';
endwhile;
}
wp_reset_query();

Actually it displays the projects filtered by category, but the name__like is not working.

Read More

Any suggestions to fix this?

Related posts

2 comments

  1. Revisited and simplified answer:

    You can try:

    $args = [
        'post_type'     => 'post',
        'post_status'   => 'publish',
        'category_name' => 'projects',
        '_name__like'   => 'proj*'         // <-- our new input argument!
    ];
    $my_query = new WP_Query( $args );
    

    where we’ve created the _name__like input argument. It supports wildcard *, for example:

        '_name__like'    => 'a*b*'        
    

    Note that draft posts don’t have post_name set before they’re published.

    We use the following plugin to support this new argument:

    /**
     * Plugin Name: Support for post name like in WP_Query
     * Description: Uses the _name__like argument and supports wildcard *.
     * Plugin URI:  http://wordpress.stackexchange.com/a/136758/26350
     * Author:      Birgir Erlendsson (birgire)
     * Version:     0.0.1
     */
    
    add_filter( 'posts_where', function( $where, $q )
    {
        if( $name__like = $q->get( '_name__like' ) )
        {
            global $wpdb;
            $where .= $wpdb->prepare(
                " AND {$wpdb->posts}.post_name LIKE %s ",
                str_replace( 
                    array( '**', '*' ), 
                    array( '*',  '%' ),  
                    mb_strtolower( $wpdb->esc_like( $name__like ) ) 
                )
            );
        }       
        return $where;
    }, 10, 2 );
    
  2. I did not use any name__like or filters. This is how I finally made it:

    $categoria = $_GET['categoria'];
    $filtro = $_GET['texto'];
    
    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $categoria
            )
        )
    );
    
    $my_query = null;
    $my_query = new WP_Query( $args );
    
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();
    $postid = $_GET['id'];
    $content_post = get_post($postid);
    $contenido= $content_post->post_content;
    $titulo= $content_post->post_title;
    
    if($filtro!="") {
        $tit=strtoupper($titulo); 
        $fil=strtoupper($filtro); 
        $con=strtoupper($contenido);
        if ((strpos($tit,$fil) !== false) || (strpos($con,$fil) !== false) ) {
            echo '<div class="Entradas">'.$titulo.'</div>';
        }
    } else {
        echo '<div class="Entradas">'.$titulo.'</div>';
    }
    endwhile;
    echo '[|Contador|]'.$my_query->post_count;
        //this is for displaying the number of posts
     }
    wp_reset_query();
    

    So I get the posts and I check if their title or content contains the value I´m looking for, Im sure there must be solutions using WP_Query options but I prefer this way.

Comments are closed.