Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 206 Vote(s) - 3.6 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exclude wordpress page template(custom template) from search results?

#1
I created custom page template.


<?php
/*
* Template Name: foo
*/
?>

This file name is "foo.php".

I tried

global $query_string;
query_posts($query_string . "&post_type=post");

But all pages will be excepted....

How to exclude only this page template from wordpress search results?

Reply

#2
Try this:

global $wp_query;
$args = array_merge($wp_query->query, array(
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'foo.php',
'compare' => '!='
)
),
));
query_posts( $args );
Reply

#3
Thanks Nikolay! For some reason last night I just was not getting this to work but today, after another hour or two, I did. It may have simply been the fact that I was using the wrong filter or was missing the last line of your code.

In my case I wanted to exclude content based upon multiple templates, so, added more key/value/compare array elements. I also only wanted to do this during a search, so, added a conditional clause for that. Here's the complete function I added to my theme's functions.php file:

// exclude any content from search results that use specific page templates
function exclude_page_templates_from_search($query) {

global $wp_the_query;

if ( ($wp_the_query === $query) && (is_search()) && ( ! is_admin()) ) {

$args = array_merge($wp_the_query->query, array(
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'page-template-1.php',
'compare' => '!='
),
array(
'key' => '_wp_page_template',
'value' => 'page-template-2.php',
'compare' => '!='
),
array(
'key' => '_wp_page_template',
'value' => 'page-template-3.php',
'compare' => '!='
)
),
));

query_posts( $args );

}

}
add_filter('pre_get_posts','exclude_page_templates_from_search');
Reply

#4
For anyone whom stumbles on this thread and doesn't succeed on WP newer versions: the $query args must be set instead redoing query_posts... as the follows:

// exclude any content from search results that use specific page templates
function exclude_page_templates_from_search($query) {

global $wp_the_query;
if ( ($wp_the_query === $query) && (is_search()) && ( ! is_admin()) ) {

$query->set(
'meta_query',
array(
array(
'key' => '_wp_page_template',
'value' => 'page-template-1.php',
'compare' => '!='
)
)
);
}

}
add_filter('pre_get_posts','exclude_page_templates_from_search');
Reply

#5
The query mentioned [by Nicolay][1] is very handy, but it also removes all posts from the search results, because posts do not contain the `'_wp_page_template'` key. To have all pages (sans the filtered template) as well as all posts you need to do the following:

// exclude any content from search results that use specific page templates
function exclude_page_templates_from_search($query) {
global $wp_the_query;
if ( ($wp_the_query === $query) && (is_search()) && ( ! is_admin()) ) {
$meta_query =
array(
// set OR, default is AND
'relation' => 'OR',
// remove pages with foo.php template from results
array(
'key' => '_wp_page_template',
'value' => 'foo.php',
'compare' => '!='
),
// show all entries that do not have a key '_wp_page_template'
array(
'key' => '_wp_page_template',
'value' => 'page-thanks.php',
'compare' => 'NOT EXISTS'
)
);
$query->set('meta_query', $meta_query);
}
}
add_filter('pre_get_posts','exclude_page_templates_from_search');

Extensive info on this can be found [in the WordPress Codex][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#6


I had to exclude more than one page template, so I had to adapt the above code a little bit, but in the end, this worked for me:

function exclude_page_templates_from_search($query) {
global $wp_the_query;
if ( ($wp_the_query === $query) && (is_search()) && ( ! is_admin()) ) {
$meta_query =
array(
// set OR, default is AND
'relation' => 'OR',
// remove pages with foo.php template from results
array(
'key' => '_wp_page_template',
'value' => array('page-landings-new.php', 'page-landings-EU.php', 'page-thankyou.php'),
'compare' => 'NOT IN'
),
// show all entries that do not have a key '_wp_page_template'
array(
'key' => '_wp_page_template',
'value' => 'page-thanks.php',
'compare' => 'NOT EXISTS'
)
);
$query->set('meta_query', $meta_query);
}
}
add_filter('pre_get_posts','exclude_page_templates_from_search');

Maybe it is useful to somebody out there.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through