• alitheabdullah

    (@alitheabdullah)


    Hi, I created an Ajax search where users can search custom post types and the results will automatically come up, here is the code. I need to know what changes I can make to this code so it searches WordPress Users by their names and some custom post types that I have created.
    Thanks

    add_action('wp_ajax_nopriv_data_fetch','data_fetch');
    function data_fetch(){
    
        $the_query = new WP_Query( array( 'posts_per_page' => 10, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => array ('capabilities' , 'industry-users') ) );
        if( $the_query->have_posts() ) :
            while( $the_query->have_posts() ): $the_query->the_post(); ?>
    
                <a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a>
    
            <?php endwhile;
    		wp_reset_postdata();  
    	else: 
    		echo '<h3>No Results Found</h3>';
        endif;
    
        die();
    }
Viewing 1 replies (of 1 total)
  • Hugh Lashbrooke

    (@hlashbrooke)

    Because WordPress users are not stored as posts, you cannot search both posts and users in a single query. To search users, you will need to use WP_User_Query or get_users.

    You could do two separate queries and then use a tool like PHP’s array_merge to merge them into a single array, but you would need to make sure to match up all the array elements since they would have different labels for users and posts.

    All of this would be relatively complex but not impossible – good luck!

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress Users Search’ is closed to new replies.