Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

maybe I'm thinking about this the wrong way, but I need to: A) exclude posts in queries site-wide (minus frontpage and navbar) based on a comparison with a meta_value, and B) orderby a differnt meta_value_num, then title

this works:

function my_get_posts( $query ) {
    $today = current_time( 'timestamp', 0 );
    if ( ! is_admin() && ! is_home() && ! $query->is_page('Locations')) {
        $query->set( 'meta_key', 'wpcf-post-weight' );
        $query->set( 'orderby', 'meta_value_num title' );
        $query->set( 'order', 'ASC' );

        $meta_query = array(
            array(
                'key' => 'wpcf-news-expire-date',
                'value' => $today,
                'compare' => '>='
            )
        );

        $query->set( 'meta_query', $meta_query );
    }

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

But, since pre_get_posts can not use is_front_page() as a conditional, I'm a little stuck about how to exclude the front page. Would changing the hook to parse_query, then using is_front_page() be the way to go?

Also, I notice that the pages this function gets called on no longer have the nav menu generated by Appearance>Menus. I can only assume this is because the menu is generated with a query. I have no idea how to exclude the nav query in particular.

EDIT: Just noticed this seems to work on every page but the frontpage to exclude the nav menu $query->query_vars['post_type'] != 'nav_menu_item' Would there be any reason that the function has access to the query_vars being passed to WP_Query on every page except the front page? Only frontpage is throwing this error:

<b>Notice</b>:  Undefined property: WP_Query::$post in <b>...wp-includes/query.php</b> on line <b>3034</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3402</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3404</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3406</b><br />
<br />
<b>Notice</b>:  Undefined property: WP_Query::$post in <b>...wp-includes/query.php</b> on line <b>3034</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3402</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3404</b><br />
<br />
<b>Notice</b>:  Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3406</b><br />
<br />
<b>Notice</b>:  Undefined index: post_type in <b>...lib/custom.php</b> on line <b>1051</b>

and lastly,...sadly,...is_main_query() just won't work. (we found ourselves in some sticky situations that required some secondary queries.)

Any help or thoughts would be greatly appreciated. Thank you.

share|improve this question
    
WP_Query does have is_front_page() method, why do you reason it cannot be used? –  Rarst Sep 12 '13 at 13:49
    
primarily because it doesn't seem to be working when i add it as a conditional, and secondarily, because this states that because pre_get_posts runs before WP_Query, is_front_page() is not available. –  sosukeinu Sep 12 '13 at 13:55
    
According with the preg_get_posts() documentation, is_front_page() won't work in the pre_get_posts() action hook. is_home() will do. –  cybnet Sep 12 '13 at 14:17
    
@cybnet unfortunately have a static frontpage as opposed to home. –  sosukeinu Sep 12 '13 at 14:25
    
@sosukeinu I just saw that you both was talking about is_front_page() and I wanted to clarify this aspect. –  cybnet Sep 12 '13 at 14:26
add comment

1 Answer

up vote 0 down vote accepted

Just found this to work to exclude the frontpage !$query->get( 'page_id' ) == get_option( 'page_on_front' ) thanks to this

seems kind of "hack-y," but it works.

to exclude the nav-menu query $query->query_vars['post_type'] != 'nav_menu_item'

Thank you everyone for your help.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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