Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Unlimited search results with filter by ID WordPress

  • SOLVED

Hello,

I am trying to get unlimited search results on my search page.

Normally on stand alone search.php page, I just write this before my loop.

<?php $posts=query_posts( $query_string . '&posts_per_page=-1' ); ?>

And then place this in my functions.php to filter out specific posts...

// SEARCH FILTER
function fb_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {

$query->set('post__not_in', array(937, 553, 1557, 434, 1608, 73, 77, 150 ));

}
return $query;
}
add_filter( 'pre_get_posts', 'fb_search_filter' );



So all of the above works... on a normal search.php template setup.


---------


How ever my search is setup up a bit different. I have two search templates. Which I am referring using the search.php


I have two types of search form on my site:

This is my first search form...

// SEARCH FORM HEADER
function search_form_header( $form ) {
$form = '<form method="get" id="header-searchform" action="' . home_url( '/' ) . '" >
<input type="text" name="s" id="search-header" value="Search" />
</form>';
return $form;
}
add_filter( 'get_search_form', 'search_form_header' );



and this is my second search form...

// SEARCH FORM EXHIBITOR
function search_form_exhibitor( $form ) {
$form = '<form method="get" id="exhibitor-searchform" action="' . home_url( '/' ) . '" >
<input type="text" name="s" id="search-exhibitor" value="Search" />
<input type="hidden" name="post_type" value="exhibitor" />
</form>';
return $form;
}



These simply use the search.php, but my search php contains this...

<?php
/**
* @package WordPress
* @subpackage
*/

$search_refer = $_GET["post_type"];

if ( $search_refer == 'exhibitor') {

load_template(TEMPLATEPATH . '/search-exhibitor.php');

} else {

load_template(TEMPLATEPATH . '/search-global.php');

};

?>



Which then uses refers you to different search templates depending on what search form you used to do the seacrh...

<strong>// SEARCH FORM HEADER</strong> uses...
search-global.php- this returns everythings.. post, pages, exhibitor

<strong>// SEARCH FORM EXHIBITOR</strong> uses...
search-exhibitors.php- this returns exhibitor post type search results


So when I using this setup, my original // SEARCH FILTER still function works 100%, but the code below does not work when I placed it above my loop in my search templates

<?php $posts=query_posts( $query_string . '&posts_per_page=-1' ); ?>



It basically makes all the search results the same what ever you search for, and shows 10 results.

Can anyone help me understand why?


Thanks

Right answer gets the money

Answers (5)

2012-08-18

Martin Pham answers:

Hi Josh,
please insert this into functions.php

add_filter('parse_query', 'my_modified_search_query');
function my_modified_search_query( $query ) {
if (is_search()) {
$query->set('post__not_in', array(937, 553, 1557, 434, 1608, 73, 77, 150));
$query->set('posts_per_page', -1);
}
return $query;
}

I tried on localhost, it works very well.

Regrads,
Martin


Josh Cranwell comments:

This works perfect! Thank you.

I don't suppose you know if it possible to segregate these queries for my different search templates...

So for example on both my search templates it runs this...


add_filter('parse_query', 'my_modified_search_query');
function my_modified_search_query( $query ) {
if (is_search()) {

$query->set('post__not_in', array(937, 553, 1557, 434, 1608, 73, 77, 150));
$query->set('posts_per_page', -1);

}
return $query;
}


And then on my search-exhibitor template, it also includes this...


$query->set('orderby', 'title');


If its impossible then don't worry

Thanks


Martin Pham comments:

try this (functions.php)

add_filter('parse_query', 'my_modified_search_query');
function my_modified_search_query( $query ) {
if (is_search()) {
$query->set('post__not_in', array(937, 553, 1557, 434, 1608, 73, 77, 150));
$query->set('posts_per_page', -1);
$post_type = (isset($_GET["post_type"])) ? $_GET["post_type"] : '';
if($post_type == 'exhibitor') {
// add something
//$query->set('post_type', 'page');
$query->set('orderby', 'title');
}
}
return $query;
}


Josh Cranwell comments:

Perfect!!!

Thank you very much for you time.

2012-08-17

Michael Caputo answers:

I've only ever modified the search form by adding some variables into the search form tag... let me see if I can dig it up.


Josh Cranwell comments:

Well I guess that would be better, how do you normally do this? Thanks


Michael Caputo comments:

Find

<?php if (have_posts()) : ?>

and put this above it:

<?php query_posts('showposts=999999'); ?>


Michael Caputo comments:

Sorry I misunderstood your question and thought you were trying to limit your search to certain categories.

My previous response should work.


Josh Cranwell comments:

This is the problem, If I use this...

<?php query_posts('showposts=999999'); ?>


Whilst this is in my functions.php

// SEARCH FILTER
function fb_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('post__not_in', array(937, 553)); // Advertising / Sponsorship, Rules & Regulations
}
return $query;
}
add_filter( 'pre_get_posts', 'fb_search_filter' );



I just get all my posts (not pages or anything else) display in my search results.

And the the results are the same for any search. Even if I search 'foobar' - It still just returns all my posts :/


Michael Caputo comments:

You could try this on your search.php:


if( is_search() ) :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("s=$s&paged=$paged&cat=1,2,3");
endif;


where 1,2,3 is the cat's you'd like to search. you can probably use -1,-2,-3 to exclude them.


Michael Caputo comments:

In all honesty, Wordpress search is pretty crummy in my experience, i've been setting up google custom search for a lot of my clients lately. If you're interested in that, send me a message and we can talk about setting that up.


Josh Cranwell comments:

OK thanks not really what I had in mind. I know what you mean.

Can query_posts('showposts=999999'); not be built into my search filter or is that impossible?


Michael Caputo comments:

Untested:






// SEARCH FILTER

function fb_search_filter($query) {

if ( !$query->is_admin && $query->is_search) {

$query->set(
'post__not_in' => array(937, 553), // Advertising / Sponsorship, Rules & Regulations
'posts_per_page' => -1
);

}

return $query;

}

add_filter( 'pre_get_posts', 'fb_search_filter' );


Josh Cranwell comments:

Nice idea but I tried this, code only works if set is an array - but it doesnt actually do anything :/

2012-08-17

Sabby Sam answers:

Hi,
Why dont you try this plugin
http://wordpress.org/extend/plugins/simply-exclude/


Josh Cranwell comments:

The plugin does not have a valid header.

Thanks for the idea tho


Sabby Sam comments:

or try this one


// SEARCH FILTER

function fb_search_filter($query) {

if ( !$query->is_admin && $query->is_search) {

//$query->get('post__not_in', array(937, 553)); // Advertising / Sponsorship, Rules & Regulations

$query = new WP_Query( 'post__not_in=937,553&posts_per_page=-1' );

}

return $query;

}

add_filter( 'pre_get_posts', 'fb_search_filter' );


Sabby Sam comments:

Or try this one
$query = new WP_Query( array( 'post__not_in' => array( 2, 5, 12, 14, 20 ) ),'posts_per_page' => '-1', );


Josh Cranwell comments:

Hmmmm... nice idea, the code works

But it does actually filter my results anymore or show unlimited results?


// SEARCH FILTER
function fb_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {

$query = new WP_Query(array(

'post__not_in' => array( 937, 553, 1557, 434, 1608, 73, 77, 150 ),
'posts_per_page' => -1

));

}
return $query;
}
add_filter( 'pre_get_posts', 'fb_search_filter' );


Josh Cranwell comments:

I meant to say it doesn't actually filter :(


Sabby Sam comments:

Hi,
The plugin works (simply-exclude), can you do one thing ,
Go to ftp accounts and log in into the ftp and open your wordpress plugin directory.
In plugin directory you will find simply-exclude, open this folder again you found the same name folder open that folder and transfer all files into the above simply-exclude folder name .
I hope this plugin will help you.


Sabby Sam comments:

Ah yeah I got it,

can you do one thing try to search other setting in your wp setting ( eg theme, if using plugin wp page navi ) etc.

$query = new WP_Query( array( 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );

basically this query fetch all result, by default it sets to posts_per_page to -1.

and I am sure something other things is interrupting.


Sabby Sam comments:

Hey can you try this one

/ SEARCH FILTER

function fb_search_filter($query) {

if ( !$query->is_admin && $query->is_search) {



$query = new WP_Query(array(



'post__not_in' => array( 937, 553, 1557, 434, 1608, 73, 77, 150 ),

'showposts' => 50



));



}

return $query;

}

add_filter( 'pre_get_posts', 'fb_search_filter' );


Josh Cranwell comments:

Nah it just ignores my post exclusions


Josh Cranwell comments:

And doesnt return more that 10 posts.

It's wierd, If I remove this functions and just use...

<?php query_posts('showposts=999999'); ?>

before my search loop - all i get is posts in my search results and the search results are the same for what ever I search.


Is this normal?


Sabby Sam comments:

can you give me pm access.


Josh Cranwell comments:

It's complicated, my site is working properly I just established. I have removed the functions entirely and I can get unlimited results using this just before my search loop

<?php $posts = query_posts( $query_string . '&posts_per_page=-1' ); ?>



So I tried this any it did not work...

<?php $posts = query_posts( $query_string . '&posts_per_page=-1&post__not_in=937,553,1557,434,1608,73,77,150' ); ?>


But surely in theory something similar could work?


Josh Cranwell comments:

Or something like this - I can understand why it would be so hard to use these both together? This doesn't work.

<?php

$exclude = array (

'posts_per_page' => -1,
'post__not_in' => array(937,553,1557,434,1608,73,77,150)

);

$args = array_merge( $query_string, $exclude );

$posts = query_posts( $args );

?>


Sabby Sam comments:

<?php

$exclude = array (


'post__not_in' => array(937,553,1557,434,1608,73,77,150)

);

$args = array_merge( $query_string."&posts_per_page= -1", $exclude );

$posts = query_posts( $args );

?>


Josh Cranwell comments:

I dont think this type of query works with search. I'm using it on a standard search.php page and it does work, just returns same results for any search.

But, on my standalone search page, this works...

// SEARCH FILTER
function fb_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('post__not_in', array(937, 553, 1557, 434, 1608, 73, 77, 150 ));
}
return $query;
}
add_filter( 'pre_get_posts', 'fb_search_filter' );


inconjunction with this...

<?php $posts=query_posts( $query_string . '&posts_per_page=-1' ); ?>

----

How ever my search is setup up a bit different. I'm not using a standard search.php - I have two types of search form on my site.

This is my first search form...

// SEARCH FORM HEADER
function search_form_header( $form ) {
$form = '<form method="get" id="header-searchform" action="' . home_url( '/' ) . '" >
<input class="searchform-mid clearit" type="text" name="s" id="search-header" value="Search" />
</form>';
return $form;
}
add_filter( 'get_search_form', 'search_form_header' );


and this is my second search form...

// SEARCH FORM EXHIBITOR
function search_form_exhibitor( $form ) {
$form = '<form method="get" id="footer-searchform" action="' . home_url( '/' ) . '" >
<input class="searchform-mid clearit" type="text" name="s" id="search-footer" value="Search" />
<input type="hidden" name="post_type" value="exhibitor" />
</form>';
return $form;
}


These simple use the search.php, but my search php contains...

<?php
/**
* @package WordPress
* @subpackage
*/

$search_refer = $_GET["post_type"];

if ( $search_refer == 'exhibitor') {

load_template(TEMPLATEPATH . '/search-exhibitor.php');

} else {

load_template(TEMPLATEPATH . '/search-global.php');

};

?>


Which then uses different search templates...

<strong>search-global.php</strong>- this returns everythings.. post, pages, exhibitor

<strong>search-exhibitors.php</strong>- this returns exhibitor post type search results


So when I using this setup, my original // SEARCH FILTER function works, but the code below is not...

<?php $posts=query_posts( $query_string . '&posts_per_page=-1' ); ?>

The code above breaks it... but does not break it if I use the no search referrer template.


I told you it was complicated :/


Josh Cranwell comments:

Sorry, typo...

I dont think this type of query works with search. I'm using it on a standard search.php page and it <strong>does not work</strong>, just returns same results for any search.

2012-08-18

Arnav Joy answers:

try this

<?php $posts=query_posts( $query_string . post_type=exhibitor&posts_per_page=-1' ); ?>

2012-08-18

Hai Bui answers:

Can you show me the code in search-exhibitor.php?