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

Limiting Search Results to One Page and Its Children Only WordPress

  • SOLVED

Hi All,

I am looking for a script that will only show search results from one page and all of its children.

I have been using the following working code to <strong>exclude</strong> an ID from the search.php results. However:

- I need to the script to either <strong>INCLUDE</strong> instead of <strong>EXCLUDE</strong> an ID and all of its children or
- give me the ability to exclude more than one ID and their children.


$parent = 1041; // 'n' for the ID of the parent page to exclude - I need to find a way to list more than one

$children_data = get_pages( array(
'child_of' => $parent,
'post_type' => 'page'
) );

$excludes = array($parent);

foreach( $children_data as $child ) {
array_push($excludes, $child->ID);
}

global $wp_query;
query_posts(
array_merge(
array('post__in' => $excludes),
$wp_query->query
)
);

if (have_posts()) : ...etc


Cheers

Answers (4)

2012-06-15

Arnav Joy answers:

try this with all of your code


<?php

$parent = array(1041,1042,1043,1044); // 'n' for the ID of the parent page to exclude - I need to find a way to list more than one


for($i = 0;$i<count($parent); $i++) {

$children_data = get_pages( array(

'child_of' => $parent[$i],

'post_type' => 'page'

) );



$excludes = array($parent[$i]);



foreach( $children_data as $child ) {

array_push($excludes, $child->ID);

}

}

?>


Arnav Joy comments:

// you can alos try following code in functions.php and nothing needed in search.php then



function filter_where($where = '') {
if ( is_search() ) {


$parent = array(1041,1042,1043,1044);


for($i = 0;$i<count($parent); $i++) {

$children_data = get_pages( array(

'child_of' => $parent[$i],

'post_type' => 'page'

) );



$excludes = array($parent[$i]);



foreach( $children_data as $child ) {

array_push($excludes, $child->ID);

}

}

for($x=0;$x<count($excludes);$x++){
$where .= " AND ID != ".$excludes[$x];
}
}
return $where;
}
add_filter('posts_where', 'filter_where');


Arnav Joy comments:


<?php
// FOR INCLUDE ONLY SINGLE PAGE AND ITS CHILD PAGE YOU CAN TRY FOLLOWING CODE IN FUNCTIONS.PHP AND REMOVE ALL THE CODE FROM SEARCH.PHP

function filter_where($where = '') {
if ( is_search() ) {

$parent = 1041;

$children_data = get_pages( array(

'child_of' => $parent,

'post_type' => 'page'

) );



$include = array($parent);



foreach( $children_data as $child ) {

array_push($include, $child->ID);

}

$includeIDS = @implode(',',$include);

$where .= " AND ID IN( ".$includeIDS." )";

}
return $where;
}
add_filter('posts_where', 'filter_where');


?>


Arnav Joy comments:

//here is another query which works with excluding more then one pages and child pages , let me know if you want include query

// search filter
function fb_search_filter($query) {
if ( !$query->is_admin && $query->is_search) {
$pages = array(2, 40, 9); // id of page or post
// find children to id
foreach( $pages as $page ) {
$childrens = get_pages( array('child_of' => $page, 'echo' => 0) );
}
// add id to array
for($i = 0; $i < sizeof($childrens); ++$i) {
$pages[] = $childrens[$i]->ID;
}
$query->set('post__not_in', $pages );
}
return $query;
}
add_filter( 'pre_get_posts', 'fb_search_filter' );


leannekera comments:

Sorry Arnav the code doesn't seem to make any difference. I do need the code to be added via the template as I will be having different search results for each page.

So each page/section has its own specific search.


Arnav Joy comments:

then try this in your page


<?php

$parent = 1041; //or $parent = get_the_ID(); // to get the current page id

$children_data = get_pages( array('child_of' => $parent,'post_type' => 'page') );
$includes = array($parent);

foreach( $children_data as $child )
array_push($includes, $child->ID);


query_posts(array('post__in' => $includes , 'post_type' => 'page'));


if (have_posts()) : ...etc

?>


leannekera comments:

I'm getting a server error page... cant see the issue.


leannekera comments:

Got it working and it works! ARNAV YOU ARE A LEGEND!


Arnav Joy comments:

try this or can you show me the url

<?php

$parent = 1041;
//or to get the current page id

//$parent = get_the_ID();


$children_data = get_pages( array('child_of' => $parent,'post_type' => 'page') );
$includes = array($parent);

foreach( $children_data as $child )
array_push($includes, $child->ID);


query_posts(array('post__in' => $includes , 'post_type' => 'page'));


if (have_posts()) :
while(have_posts()) : the_post();

the_title();
endwhile
endif;

?>


Arnav Joy comments:

Thanks , leannekera

do not forget to vote

2012-06-15

Francisco Javier Carazo Gil answers:

If you have an array with all posts (or all his IDs) and another array with only excluded posts, you can make a diff between them:

http://es.php.net/manual/es/function.array-diff.php

2012-06-15

Hai Bui answers:

I don't understand. Your current code seems to limit the search results to an a page and its children, NOT <strong>exclude</strong> them from search results, how can it <strong>exclude</strong> posts with "post__in" parameter??

2012-06-15

Sébastien | French WordpressDesigner answers:

to exclude an array, juste use 'post__not_in' => array(264, 234),
in this case page 264 & 234are excluded


Sébastien | French WordpressDesigner comments:

so : just replace post__in by post__not_in
easy ! :-)


leannekera comments:

Yeah I thought that but my first attempt didn't work... give me a sec I will try again.


Sébastien | French WordpressDesigner comments:

use this code if your code doesn't work
$parent = 1041; // 'n' for the ID of the parent page to exclude - I need to find a way to list more than one



$children_data = get_pages( array(

'child_of' => $parent,

'post_type' => 'page'

) );



$excludes = array();
$excludes[] = $parent;



foreach( $children_data as $child ) {

$excludes[] = $child->ID;

}



global $wp_query;

query_posts('post__not_in' => $excludes);



if (have_posts()) : ...etc


leannekera comments:

I changed the code to the following and it still shows results from ID 1041 and 1042. Ideally I want it to show the results from 1042 and its children only but even if I change the ID it still shows them...



if (have_posts()) :

$args = array(
'post_type' => 'page',
'post__not_in' => array(1041,1042),
);
query_posts( $args ); ?>