• Resolved ringostar77

    (@ringostar77)


    Hi there!

    I am using a custom search function to only display images when using a search form.

    Unfortunately I don’t know how to use the plugins index within my search function.

    This is what I have in my functions.php:

    add_action('pre_get_posts', 'attachment_search');
    
    function attachment_search( $query ) {
        if ($query->is_search) {
    		$query->set( 'post_type', array( 'post', 'attachment' ) );
    		$query->set( 'post_status', array( 'publish', 'inherit' ) );
    		$query->set( 'posts_per_page', 1000);
        }
        return $query;
    }

    This is what I have in my search.php:

    <div id="content" class="content" role="main">
    <?php
    if ( have_posts() ) :
    	while ( have_posts() ) : the_post();
    		$a[] = get_the_ID();
    	endwhile;
    	
    	$ids_comma = implode( ",", $a );
    	
    	echo do_shortcode('[vc_row full_width="stretch_row_content"][vc_column][dt_gallery_masonry bwb_columns="desktop:5|h_tablet:4|v_tablet:3|phone:2" image_border_radius="0px" project_icon_color="#ffffff" project_icon_border_width="0px" include="'.$ids_comma.'" loading_mode="js_pagination" jsp_posts_per_page="60" jsp_show_all_pages="y" jsp_gap_before_pagination="16px"][/vc_column][/vc_row]');
    ?>
    <?php else : get_template_part( 'no-results', 'search' ); endif; ?>
    </div>

    Sometimes there are over 1000 attachments to show so my current search function takes about 10 to 15 seconds to get all the image ID’s :/

    I would really appreciate your help!

    • This topic was modified 4 years, 8 months ago by ringostar77.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Epsiloncool

    (@epsiloncool)

    Hi

    in case you need only IDs of your posts, you need to specify this in the WP_Query() parameters:

    ‘fields’ => ‘ids’,

    This may sufficiently increase performance.

    Also, you may check which part of your code took so much time actually (maybe it’s not WP_Query()).

    $t0 = microtime(true);

    …some piece of your code

    $t1 = microtime(true);

    echo ‘Elapsed time: ‘.sprintf(‘%.4f’, $t1 – $t0);

    Also, you may decide to paginate your view (because usually, people do not see all 1000 images at the same time). You can request smaller parts of 20 or 50 images and this will be significantly faster.

    Plugin Author Epsiloncool

    (@epsiloncool)

    I guess you have resolved this issue.
    Thanks for the report!

    Thread Starter ringostar77

    (@ringostar77)

    Yes sir, thank you very much for your help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Include WPFTS in custom search function’ is closed to new replies.