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 im 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. How can i fix it?? Thank you.

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

2 Answers 2

Extend the WP_Query class:

You can try:

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

where we use an extended version of the WP_Query class, namely:

/**
 * Class WPSE_Query
 *
 * Add a support for the name__like parameter
 *
 * @link http://wordpress.stackexchange.com/a/136758/26350
 *
 */
class WPSE_Query extends WP_Query 
{       
    public function __construct( $args = array() )
    {
        add_filter( 'posts_where', array( $this, 'posts_where' ), 10, 2 );
        parent::__construct( $args );
    }
    public function posts_where( $where, $qry )
    {       
        remove_filter( current_filter(), array( $this, __FUNCTION__ ) );    
        $name__like = $qry->get( 'name__like' ); 
        if( ! empty( $name__like )  )
        {
            $where .= " AND ";
            $where .= $GLOBALS['wpdb']->posts;
            $where .= ".post_name LIKE '";
            $where .= esc_sql( like_escape( $name__like ) );
            $where .= "%' ";            
        }   
        return $where;
    }
}

You can then modify this to your needs.

You could for example use another name for the parameter name__like, in case it gets supported by WP_Query() in the future.

Hope this help.

share|improve this answer
    
Thanks for your feedback birgire. Im close to get the solution. –  Mikel Mar 4 at 14:18
    
Are you searching for %Proj% or Proj%? –  birgire Mar 4 at 20:41
    
@Mikel did you make any progress regarding the problem? –  birgire Mar 11 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 yesterday
    
Great to hear you found it somewhat useful ;-) –  birgire yesterday
up vote 0 down vote accepted

sorry for no answering before, I completely forgot it:

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. Sorry for my late feedback :). Thanks all for your comments.

share|improve this answer
    
Thanks for reporting back. You should then accept your answer to close the question. –  birgire Apr 4 at 11:05
    
Done!! Thank you birgire :) –  Mikel Apr 4 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.