Support » Plugin: CommentPress Core » Sort Search Results in Ascending Order?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Christian Wach

    (@needle)

    From a glance at your site (nice project, btw), I assume you mean you want to restrict results to pages in ascending menu order. Something along the lines of the following should get you started…

    /**
     * Filter search query to order pages by menu_order
     */
    function my_search_filter( $query ) {
    
    	// restrict to search outside admin
    	if ( ! is_admin() AND $query->is_main_query() AND $query->is_search ) {
    
    		// restrict to pages
    		$query->set( 'post_type', 'page' );
    
    		// order by ascending menu_order
    		$query->set( 'orderby', 'menu_order' );
    		$query->set( 'order', 'ASC' );
    
    	}
    
    }
    
    add_filter( 'pre_get_posts', 'my_search_filter' );

    Much more info on the ‘pre_get_posts’ filter here:
    https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    If your menu is a custom menu, you may still need to specifically order your pages. There are plenty of plugins that will help you do this, for example:
    https://wordpress.org/plugins/simple-page-ordering/

    Cheers, Christian

    Thank you for the reply! Yes, I do mean pages. So, for example with my site, I would like results from Chapter I to be displayed first, then Chapter II, etc.

    I am very new to putting code into a WordPress site. Where should I place the code you gave me? I would assume in the search.php, but from there I am lost.

    Plugin Author Christian Wach

    (@needle)

    I presume from what you say that you’re not using a CommentPress child theme. If that’s the case then you should put the code in a plugin. It’s quite simple and a good way to start learning about WordPress. There are a huge number of resources out there to help you. For example, the first couple of hits from a Google search are:

    https://codex.wordpress.org/Writing_a_Plugin
    http://corpocrat.com/2009/12/27/tutorial-how-to-write-a-wordpress-plugin/

    All you need is the most basic plugin – the plugin header and the code I posted above will do.

    If, on the other hand, you are using a child theme (which I would recommend) then you should put the code into ‘functions.php’. You can get a starter child theme for CommentPress here:

    https://github.com/christianwach/commentpress-modern-child

    Cheers, Christian

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort Search Results in Ascending Order?’ is closed to new replies.