0

With below code working for searching with post title and post content. And also after installing plugin 'ACF: Better Search', the post is also searching from the ACF fields and give search result on search result div element.

But now I also want to search the post with Author Name and with other custom added author meta values.

[WordPress 4.9.2]

Tried 'WP Extended Search — WordPress Plugins' plugin but did not worked.

// ajax hook for searching custom post type

add_action( 'wp_ajax_search_cposttype', 'searchCposttypeByTerm' );
add_action( 'wp_ajax_nopriv_search_cposttype', 'searchCposttypeByTerm' );

function searchCposttypeByTerm() {
//$authors=get_users();
$wp_query = new WP_Query(array(
    'post_type'=>'cposttype',
    'post_status'=>'publish',
    's' => $_GET['searchTerm']));

while ($wp_query->have_posts()) : $wp_query->the_post();
    get_template_part('template-parts/cposttype/cposttype-post');
echo '<pre>';
                //print_r($wp_query);
                echo '</pre>';
endwhile;
wp_die();

}

Please could you help me. It will be better without using plugin.

Thanks

0

You can start with prepare information what you need using MySQL

SELECT * FROM `wp_usermeta` WHERE `meta_key` = "description"

For example, you can create the file with the prepared output of your custom query and then use jQuery * Ajax for your search box..

I've show you sample of json file:

<?php
require_once('../../../wp-load.php');


$gm = array("results"  => array());
$loop = new WP_Query( array( 'post_type' => array('post', 'page'), 'post_status' => 'publish') );
if ( $loop->have_posts() ): while ( $loop->have_posts() ): $loop -> the_post();
    $tmp = array(
        "title" => get_the_title(),
        "url" => get_the_permalink(),
        "text" => get_the_excerpt()
    );
    array_push($gm["results"],$tmp);
endwhile;
endif;

echo json_encode($gm);

Unlikely, HERE in WP Query Author parameters you can grab only post related to author.

So the best solution is just simple querys to SQL and output it in custom php file. Then easily you can show this info in your searchbox:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.