Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Any suggestions to fix this?

share|improve this question
    
The name__like parameter is not supported by the WP_Query(). –  birgire Mar 4 '14 at 12:29

2 Answers 2

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 );
share|improve this answer
    
Thanks for your feedback birgire. Im close to get the solution. –  Mikel Mar 4 '14 at 14:18
    
Are you searching for %Proj% or Proj%? –  birgire Mar 4 '14 at 20:41
    
@Mikel did you make any progress regarding the problem? –  birgire Mar 11 '14 at 19:48
1  
Reused your code in an answer. Can't upvote you again unfortunately as I already did a while ago :-). Great code. Thanks –  Pieter Goosen Aug 26 '14 at 16:03
    
Great to hear you found it somewhat useful ;-) –  birgire Aug 26 '14 at 16:37
up vote 0 down vote accepted

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.

share|improve this answer
    
Thanks for reporting back. You should then accept your answer to close the question. –  birgire Apr 4 '14 at 11:05
    
Done!! Thank you birgire :) –  Mikel Apr 4 '14 at 11:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.