1

I have a custom WP_Query with the following arguments

$args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'meta_key' => 'tile-home',
    'meta_value' => 1,
    'orderby' => 'menu_order',
    'order' => 'ASC'
);

That selects all the published pages which have the custom field tile-home set to 1 and orders them by the page attribute Order.

How can I edit the arguments in order (please, forgive me the pun) to exclude all the pages that have menu_order set to 0?

2

You can define custom where like this if your table is wp_posts

function _20170112( $where ){    

  $where = $where . ' AND wp_posts.menu_order > 0 ';             
  return $where;
}

And sandwich your query with this:

$args = array(      
    'post_type' => 'page',
    'post_status' => 'publish',
    'meta_key' => 'tile-home',
    'meta_value' => 1,
    'orderby' => 'menu_order',
    'order' => 'ASC'
);


add_filter( 'posts_where', '_20170112' );
$q = new WP_Query($args);
remove_filter( 'posts_where', '_20170112' );
  • I believe your filter needs to take the table prefix into account like this; function foo( $where ) { global $wpdb; return $where . " AND $wpdb->posts.menu_order > 0 "; } – Dan May 29 '18 at 11:36

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.