WordPress.org

Ready to get started?Download WordPress

Forums

parse_query filter changed in 3.1 ? (11 posts)

  1. alex chousmith
    Member
    Posted 3 years ago #

    in previous versions of WP (3.0.5), i had been able to list all Pages with a given Page Template (meta_key _wp_page_template = "mytemplate.php") by adding a filter to the "parse_query" . the following code in my theme's functions.php made it so the link /wp-admin/edit.php?s&post_type=page&mypagetemplate=mytemplate.php showed the EDIT PAGES screen, listing only Pages where the _wp_page_template = "mytemplate.php"

    add_filter( 'parse_query', 'my_pagetemplate_filter' );
    function my_pagetemplate_filter( $query ) {
        global $pagenow;
        if ( is_admin() && $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && isset( $_GET['post_type'] ) == 'page' && isset( $_GET['mypagetemplate'] ) && isset( $_GET['mypagetemplate'] ) == 'mytemplate.php' ) {
            $query->query_vars['orderby'] = 'meta_value';
            $query->query_vars['meta_key'] = '_wp_page_template';
            $query->query_vars['meta_value'] = 'mytemplate.php';
        }
    }

    However, with the upgrade to 3.1, that no longer works, and the same link now just returns "No pages found."

    Can anyone provide insight as to what has changed on the $query level, if this is still possible with WP 3.1, or if I should just forget that it ever was possible?

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    It's not your fault, i've just spent the last 30 minutes testing code and checking Trac for bug reports, it's due to(as far as i can tell) the introduction of the new meta parameters, you need to replace the meta_key and meta_value args with meta_query..

    However, the usage of meta_query isn't clear and sent me in circles for a little while, i finally spotted what i was doing wrong, which was highlighted in this ticket.
    http://core.trac.wordpress.org/ticket/16563

    Remove your three $query->query_vars lines and replace them with..

    set_query_var( 'meta_query', array( array( 'key' => '_wp_page_template', 'value' => 'mytemplate.php' ) ) );
    set_query_var( 'orderby','meta_value' );

    And your function should then work as it previously did.. ;)

  3. jaisonn
    Member
    Posted 3 years ago #

    Thanks, Mark. That worked like a charm.

  4. BrenFM
    Member
    Posted 3 years ago #

    Thanks (again) t31os! That was starting to seriously bake my bacon!

  5. marymidori
    Member
    Posted 3 years ago #

    Unfortunately this still returns no results.

    /wp-admin/edit.php?s&post_status=all&post_type=inuit-firms&action=-1&m=0&sortby_status=A&paged=1&mode=list&action2=-1

    ATTEMPT ONE:

    add_filter( 'parse_query', 'status_filter' );
    
    function status_filter( $query ) {
      global $pagenow;
        if ($pagenow=='edit.php' &&
        			isset($_GET['post_type']) && $_GET['post_type']=='inuit-firms' &&
                isset($_GET['sortby_status']) && !empty($_GET['sortby_status'])) {
            set_query_var( 'meta_query', array( array( 'key' => 'if_status', 'value' => $_GET['sortby_status']) ) );
        }
     }

    ATTEMPT TWO:

    Before I was using the following which had no problems.

    add_filter( 'request', 'status_request' );
    function status_request($request) {
        $current_url = substr( $GLOBALS['PHP_SELF'], -18);
        if (is_admin() && $current_url == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type']=='inuit-firms' &&
        isset( $_GET['sortby_status'] ) && !empty( $_GET['sortby_status'] ) ) {
    
    	$request['meta_key']   = 'if_status';
    	$request['meta_value'] = $_GET['sortby_status'];
        }
        return $request;
    }

    I tried this code as well but setting $request['meta_query'], but this also found no posts.

    Any thoughts on what could be conflicting in this case?

    Thanks
    Midori

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    Try this instead..

    add_action( 'parse_query', 'status_filter' );
    
    function status_filter() {
    
    	global $pagenow, $post_type;
    
    	if( 'edit.php' != $pagenow || 'inuit-firms' != $post_type || !isset( $_GET['sortby_status'] ) )
    		return;
    
    	$meta_group = array(
    		'key' => 'if_status',
    		'value' => $_GET['sortby_status']
    	);
    
    	set_query_var( 'meta_query', array( $meta_group ) );
    }
  7. marymidori
    Member
    Posted 3 years ago #

    Unfortunately still finds no posts.

  8. Mark / t31os
    Moderator
    Posted 3 years ago #

    I've just tested the code for you and it works.

    Check your spelling, are you requesting the page with..

    ?sortby_status=term

    and not..

    sortbystatus=term

    And is your field definately called if_status and not if-status

    I can only guess it's a small error in the naming somewhere(eg. a missing or incorrect character), the code works for me.

    Is your post type name definately correct?
    Is the meta key you're requesting correct and definately applied to one of the custom post types?

  9. marymidori
    Member
    Posted 3 years ago #

    I do not what it could be. I use the following code to display the dropdown and it works, showing the correct value when selected:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I definitely have at least one post of each.

    All to say, there could very easily be a naming error somewhere, but I have no idea where!

    I'll keep looking.

    Thanks for your help,
    mido.

  10. slobizman
    Member
    Posted 3 years ago #

    I can't believe this was changed on us like this. My now-not working code seems a little different than the above. Can someone he;lp me out as to what to change?

    My Thesis function to omit some categories of posts from displaying.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  11. slobizman
    Member
    Posted 3 years ago #

    I figured it out. I'll post the change for anyone else:

    if ($query->is_home ) {
         $query->set('category__not_in', array(226,317));
       } elseif ($query->is_paged ) {
          $query->set('category__not_in', array(226,317));
       } elseif ($query->is_search ) {
          $query->set('category__not_in', array(226));
       } elseif ($query->is_archive ) {
          $query->set('category__not_in', array(226));
       } elseif ($query->is_feed ) {
          $query->set('category__not_in', array(226));
       }
       return $query;

Topic Closed

This topic has been closed to new replies.

About this Topic