Manohar singh
Manohar singh

Reputation: 509

List custom post type by custom taxonomy id and filter by array on post id

Hi I need to fetch result of post type by category id and post title using like operator.

For example

Category name : Sports  
Post name : Basketball , hockey , volley ball

From the below query i get post name and its id and i pass them to the get post query but the query returns all post inside that category

For suppose i search for volleyball inside that category i get all results i need only the volleyball in the output.

See My code below

$mypostids_title = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$title%' ");

$args_sby= array(
    'post_type'      => 'campaign',
    'post_status'    => 'publish',  
    'posts_per_page' => -1,    
    'post__in' => $mypostids_title, 
        'tax_query' => array(
            array(
           'taxonomy' => 'campaign_category',
           'field'    => 'term_id',                      
            'terms'    => $_GET['c'],
            'operator' => 'AND',                
            )
       ),

    );
$posts = get_posts($args_sby);

I get array of post id from the above select query and i pass them inside the get post query argument and i get only result by taxonomy but i need to get result by taxonomy as well as the post id , please let me know how to solve this. Thanks in advance.

Upvotes: 0

Views: 5327

Answers (3)

Shah Ankit
Shah Ankit

Reputation: 129

Try this.It might helps you.

$args_sby= array(
    'post_type'      => 'campaign',
    'post_status'    => 'publish',  
    'posts_per_page' => -1,
    'post_title'     => $title,
    'tax_query' => array(
        array(
            'taxonomy' => 'campaign_category',
            'field'    => 'term_slug',
            'terms'    => $_GET['c'],
            'operator' => 'AND',
        )
   ),
);
$posts = get_posts($args_sby);

After this please put the below code in your theme's function.php file.

//Filter for post title.
function title_filter( $where, &$wp_query ){
    global $wpdb;
    if ( $search_term = $wp_query->get( 'post_title' ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like( $search_term) . '%\'';
    }    
    return $where;
}

add_filter( 'posts_where', 'title_filter', 10, 2 );

Upvotes: 1

Sudhir Tiwari
Sudhir Tiwari

Reputation: 156

Try below code, it could be helpful. You can directly pass your search keyword and category in the query_posts.

 $args_sby= array(
    'post_type'      => 'campaign',
    'post_status'    => 'publish',  
    'posts_per_page' => -1,    
    's' => $keywords, 
    'taxonomy' => 'campaign_category',
    'term' => 'yourterm' 
    );
$posts = query_posts($args_sby);

Find parameter related help here

Upvotes: 2

Shivangi Vyas
Shivangi Vyas

Reputation: 117

$custom_terms = get_terms('custom_taxonomy');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
     }

you can try this code

Upvotes: 1

Related Questions