results count made public

This topic contains 1 reply, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 2 years, 7 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #34331
    ekoklas92
    ekoklas92
    Participant

    Hi,

    I have searched the first 25 forum pages to find what I am looking for before I posted this, and kinda found it, but I would like your confirmation please!
    [related forum article] https://wp-dreams.com/forums/topic/retrieve-total-result-count-with-live-filter/

    Problem Description
    I add incremental numbering in front of each post under each category and I would like to do the same with the results of the search plugin. I cannot do that currently, as I have no idea about the number of results.

    Actions taken
    1) I have applied the
    add_filter( ‘asp_results’, ‘asp_number_results’, 10, 1 );
    snippet suggestion, but this only adds numbering to the ajax dropdown list.
    2) I have tried to use the WP search results variable $wp_query->found_posts, but it comes back empty.

    Request
    I would like to use a “results” variable from your plugin so that I can count the results on the result page.
    Or some kind of structure with the total results that I can show using a loop.
    Is there such functionality?

    Kind Regards,
    Efthymios

    #34344
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I’m afraid this might not be possible (with simple modifications at least). WordPress queries each post title separately on the fly, so changing them via the loop is often times not that simple.
    There is a custom code you can try, but there it is very likely not going to work:

    add_filter('posts_results', 'my_custom_posts_results', 9999999, 2);
    function my_custom_posts_results($posts, $query) {
    	if ( $wp_query->is_search() && $wp_query->is_main_query() ) {
    		$page = isset($_GET['paged']) ? $_GET['paged'] - 1 : 0;
    		$n = get_option( 'posts_per_page' ) * $page;
    		$n = $n < 0 ? 0 : $n;
    		foreach ( $posts as $k => &$post ) {
    			$post->post_title = ($n + $k) . '. ' . $post->post_title;
    		}
    	}
    	return $posts;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.