Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_ajax_find_posts [ WordPress Function ]

wp_ajax_find_posts ( No parameters )
Defined at:



No description yet.

Source


<?php
function wp_ajax_find_posts() {
    global 
$wpdb;

    
check_ajax_referer'find-posts' );

    if ( empty(
$_POST['ps']) )
        
wp_die();

    if ( !empty(
$_POST['post_type']) && in_array$_POST['post_type'], get_post_types() ) )
        
$what $_POST['post_type'];
    else
        
$what 'post';

    
$s stripslashes($_POST['ps']);
    
preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/'$s$matches);
    
$search_terms array_map('_search_terms_tidy'$matches[0]);

    
$searchand $search '';
    foreach ( (array) 
$search_terms as $term ) {
        
$term esc_sqllike_escape$term ) );
        
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))";
        
$searchand ' AND ';
    }
    
$term esc_sqllike_escape$s ) );
    if ( 
count($search_terms) > && $search_terms[0] != $s )
        
$search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";

    
$posts $wpdb->get_results"SELECT ID, post_title, post_status, post_date FROM $wpdb->posts WHERE post_type = '$what' AND post_status IN ('draft', 'publish') AND ($search) ORDER BY post_date_gmt DESC LIMIT 50" );

    if ( ! 
$posts ) {
        
$posttype get_post_type_object($what);
        
wp_die$posttype->labels->not_found );
    }

    
$html '<table class="widefat" cellspacing="0"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th>'.__('Date').'</th><th>'.__('Status').'</th></tr></thead><tbody>';
    foreach ( 
$posts as $post ) {

        switch ( 
$post->post_status ) {
            case 
'publish' :
            case 
'private' :
                
$stat __('Published');
                break;
            case 
'future' :
                
$stat __('Scheduled');
                break;
            case 
'pending' :
                
$stat __('Pending Review');
                break;
            case 
'draft' :
                
$stat __('Draft');
                break;
        }

        if ( 
'0000-00-00 00:00:00' == $post->post_date ) {
            
$time '';
        } else {
            
/* translators: date format in table columns, see http://php.net/date */
            
$time mysql2date(__('Y/m/d'), $post->post_date);
        }

        
$html .= '<tr class="found-posts"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' esc_attr($post->ID) . '"></td>';
        
$html .= '<td><label for="found-'.$post->ID.'">'.esc_html$post->post_title ).'</label></td><td>'.esc_html$time ).'</td><td>'.esc_html$stat ).'</td></tr>'."\n\n";
    }
    
$html .= '</tbody></table>';

    
$x = new WP_Ajax_Response();
    
$x->add( array(
        
'what' => $what,
        
'data' => $html
    
));
    
$x->send();

}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

Gebruikersdiscussies [ wordpress.org ]

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics