0

Hello I was trying to filter the pages using a custom filter in admin post listing of wordpress, but after selecting in the dropdown it returns nothing like its just showing all the posts and the dropdown text returns to its default text "Show all Templates". Please help, I just cant figure it out why its not really working.

after I click the Filter button, the url is:

wp-admin/edit.php?s&post_status=all&post_type=page&action=-1&m=0&template=default&paged=1&action2=-1

heres the screenshot:

page-template

heres my code:

function pagetemplates_request_admin($request) {
if( isset($_GET['_wp_page_template']) && !empty($_GET['_wp_page_template']) ) {
    $request['meta_key'] = '_wp_page_template';
    $request['meta_value'] = $_GET['template'];
}
return $request;}

..

function pagetemplates_restrict_manage_posts() {
global $wpdb;
$optemplate ='';
$templates = $wpdb->get_col("
    SELECT DISTINCT meta_value
    FROM ". $wpdb->postmeta ."
    WHERE meta_key = '_wp_page_template'
    ORDER BY meta_value
");
?>
 <select name="template" id="template">
     <option value="">Show all Templates</option>
     <?php foreach ($templates as $template) { ?>
     <option value="<?php echo esc_attr( $template ); ?>" <?php if(isset($_GET['_wp_page_template']) && !empty($_GET['_wp_page_template']) ) selected($_GET['_wp_page_template'], $template); ?>>

        <?php 
            $optemplate = esc_attr($template); 
            switch ($optemplate)
                {
                    case 'template1.php';
                        echo 'Blog Page Template 1';
                        break;
                    case 'template2.php';
                        echo 'Blog Page Template 2';
                        break;
                    default:
                        echo 'Default Template';
                        break;
                }
         ?>
     </option>
     <?php } ?>
 </select> 

 <?php}

...

if( is_admin() && isset($_GET['post_type']) && $_GET['post_type'] == 'page' ) {
add_filter('request', 'pagetemplates_request_admin');
add_filter('restrict_manage_posts', 'pagetemplates_restrict_manage_posts');}
0

The initial thing that jumps out is that you're using:

template=default

in the URL, but in the code you are using

if( isset($_GET['_wp_page_template']) && !empty($_GET['_wp_page_template']) ) {

you should be checking:

if( isset($_GET['template']) && !empty($_GET['template']) ) {

Also, don't forget to sanitize the $_GET as that's user input and cannot be trusted.

Hope that helps.

1
  • Hello Steven, I tested it again and removed other codes I made after I posted my code here.,IT WORKS NOW !!! And I really thank God for the help you gave...
    – Kiko Lolo
    Apr 26 '14 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.