Wordpress. pre_get_posts

Sorry for the off topic question, but I'm having real trouble understanding pre_get_posts().
I have a page that I'm creating a custom loop for a custom post type. And I want to modify the main query to change the ordering.
So I add the function I want:

function order_by_menu_order($query){

print "INSIDE MENU ORDER";
if ( !is_admin() && $query->is_main_query() ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'DESC' );
}
}
add_action('pre_get_posts','order_by_menu_order');

This is at the top of the page, before the:
if ( have_posts() ) :

I've create a custom archive page that is being called for my post type.
ANd I'm putting the code in there.

The file is called archive-department.php because I'm creating departments.

  • Alex Stine
    • System Administrator

    Hello Norman
    Hope you're well today!

    You should really create a custom query in this case as customizing the default query will change it everywhere. However you can attempt to use conditionals if you have a custom post type. Please try the code below in your functions.php file.

    function order_by_menu_order($query){
    
    print "INSIDE MENU ORDER";
    if ( !is_admin() && $query->is_main_query() && $query->query( 'post_type' ) == 'CUSTOM_POST_TYPE_NAME' ) {
    $query->set( 'orderby', 'menu_order' );
    $query->set( 'order', 'DESC' );
    }
    }
    add_action('pre_get_posts','order_by_menu_order');

    Remember to remove this function from your custom archive template before adding this modified function to functions.php.

    Hope this helps. Please let me know if I can assist you with any further questions.

    Thanks,
    Alex :smiley:

  • Norman
    • Flash Drive

    Thank you!

    Is it possible in the function to set a specific page as being the only one that the function gets applied to?

    eg. if my archive page is called archive-department.php, can I set one of the conditionals to be that page? Or even using a slug? eg. if on my site I get to that page using <site>/department/ then can I set that slug as one of the conditionals?

    One of the reasons I'm trying to do this menu_order, is because I have these pages set up in a hierarchy using the Wordpress parent-child structure. But I'd like to display them as nested pages.

    ie. Dept 1 (order 0)
    Dept 1.1 (order 0)
    Dept 1.2 (order 1)
    Dept 2 (order 1)
    Dept 2.1 (order 0)

    There's nothing obvious I can see to support this....

    Thanks,
    Norm

  • Norman
    • Flash Drive

    Hmmm... Nothing that I put into the is_page parameters works.

    I've tried

    && is_page('department')
    && is_page('departments')
    && is_page('archive-department')
    && is_page('archive-department.php')

    This is NOT an existing page as such. ie. it hasn't been created via the Pages post_type.

    It's a custom post type that is being called automatically when I go to the <site>/department/ HREF.

    Norm

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.