it's my first question on Stack network so let me know if something wrong in the use of this.
I have a theme with these pieces of code:
/**
* Get the Featured image URL of a post
* @global object $post
* @param int $post_id
* @return string
*/
function kleo_get_post_thumbnail_url( $post_id = null ) {
$image_url = '';
$thumb = get_post_thumbnail_id( $post_id );
//all good. we have a featured image
$featured_image_url = wp_get_attachment_url( $thumb );
if ( $featured_image_url ) {
$image_url = $featured_image_url;
} elseif ( sq_option( 'blog_get_image', 1 ) == 1 ) {
global $post;
if (! is_object($post) && $post_id != NULL ) {
$post = setup_postdata( get_post($post_id) );
}
ob_start();
ob_end_clean();
if (isset($post->post_content)) {
$output = preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $post->post_content, $matches);
$image_url = isset($matches[1][0]) ? $matches[1][0] : null;
}
}
//Defines a default image
if ( empty( $image_url ) ) {
$image_url = sq_option_url('blog_default_image', '');
}
return $image_url;
}
This code return the Custom featured image url if exists, the blog default image (set in theme option panel) if custom img not exists.
In this code there's not filter for post_type which means the Default image is not only for 'post', but also 'page', 'attachment', 'revision', 'nav_menu_item' and every custom post type i have.
Now the problem is in the thumbnail for post_type in search result, managed by this code:
if(!function_exists('kleo_ajax_search'))
{
function kleo_ajax_search()
{
//if "s" input is missing exit
if( empty( $_REQUEST['s'] ) && empty( $_REQUEST['bbp_search'] ) ) die();
if( ! empty( $_REQUEST['bbp_search'] ) ) {
$search_string = $_REQUEST['bbp_search'];
} else {
$search_string = $_REQUEST['s'];
}
$output = "";
$context = "any";
$defaults = array(
'numberposts' => 4,
'posts_per_page' => 20,
'post_type' => 'any',
'post_status' => 'publish',
'post_password' => '',
'suppress_filters' => false,
's' => $_REQUEST['s']
);
if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] != '' ) {
$context = explode( ",", $_REQUEST['context'] );
$defaults['post_type'] = $context;
}
$defaults = apply_filters( 'kleo_ajax_query_args', $defaults);
$the_query = new WP_Query( $defaults );
$posts = $the_query->get_posts();
$members = array();
$members['total'] = 0;
$groups = array();
$groups['total'] = 0;
$forums = FALSE;
//if there are no posts, groups nor members
if( empty( $posts ) && $members['total'] == 0 && $groups['total'] == 0 && ! $forums ) {
$output = "<div class='kleo_ajax_entry ajax_not_found'>";
$output .= "<div class='ajax_search_content'>";
$output .= "<i class='icon icon-exclamation-sign'></i> ";
$output .= __("Sorry, we haven't found anything based on your criteria.", 'kleo_framework');
$output .= "<br>";
$output .= __("Please try searching by different terms.", 'kleo_framework');
$output .= "</div>";
$output .= "</div>";
echo $output;
die();
}
//if there are posts
if( ! empty( $posts ) ) {
$post_types = array();
$post_type_obj = array();
foreach ( $posts as $post ) {
$post_types[$post->post_type][] = $post;
if (empty($post_type_obj[$post->post_type])) {
$post_type_obj[$post->post_type] = get_post_type_object($post->post_type);
}
}
foreach ($post_types as $ptype => $post_type) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-' . esc_attr( $post_type_obj[$ptype]->name ) . '">';
if (isset($post_type_obj[$ptype]->labels->name)) {
$output .= "<h4><span>" . $post_type_obj[$ptype]->labels->name . "</span></h4>";
} else {
$output .= "<hr>";
}
$count = 0;
foreach ($post_type as $post) {
$count++;
if ($count > 4) {
continue;
}
$format = get_post_format( $post->ID );
if ( $img_url = kleo_get_post_thumbnail_url( $post->ID ) ) {
$image = aq_resize( $img_url, 44, 44, true, true, true );
if( ! $image ) {
$image_url = "<i class='icon icon-link'></i>";
}
$image = '<img src="'. $image .'" class="kleo-rounded">';
} else {
if ($format == 'video') {
$image = "<i class='icon icon-video'></i>";
} elseif ($format == 'image' || $format == 'gallery') {
$image = "<i class='icon icon-picture'></i>";
} else {
$image = "<i class='icon icon-link'></i>";
}
}
$excerpt = "";
if ( ! empty($post->post_content) ) {
$excerpt = char_trim( trim(strip_tags(strip_shortcodes($post->post_content))), 40, "..." );
}
$link = apply_filters('kleo_custom_url', get_permalink($post->ID));
$classes = "format-" . $format;
$output .= "<div class ='kleo_ajax_entry $classes'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='$link' class='search_title'>";
$output .= get_the_title($post->ID);
$output .= "</a>";
$output .= "<span class='search_excerpt'>";
$output .= $excerpt;
$output .= "</span>";
$output .= "</div>";
$output .= "</div>";
}
$output .= '</div>';
}
$output .= "<a class='ajax_view_all' href='" . home_url( '/' ) . '?s=' . $search_string . "'>" . __('View all results', 'kleo_framework') . "</a>";
}
For every 'post_type' i get the same Blog Default image.
Could anyone explain to me how to filter the post_type or how to show sq_option( 'blog_get_image') only to 'post' in search results?
Thanks