0

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.

5
  • WP_Query does have is_front_page() method, why do you reason it cannot be used?
    – Rarst
    Commented Sep 12, 2013 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
    Commented Sep 12, 2013 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.
    – cybmeta
    Commented Sep 12, 2013 at 14:17
  • @cybnet unfortunately have a static frontpage as opposed to home.
    – sosukeinu
    Commented Sep 12, 2013 at 14:25
  • @sosukeinu I just saw that you both was talking about is_front_page() and I wanted to clarify this aspect.
    – cybmeta
    Commented Sep 12, 2013 at 14:26

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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