• Resolved waffeltje

    (@waffeltje)


    I have a question regarding the ordering in the [jobs] shortcode. I have a custom field names ‘order’, which contains a double which I want to use for ordering jobs in the [jobs] shortcode. How can I use this custom meta field of the custom post type ‘job_listing’ for ordering jobs?
    Next to that I still want to keep featured listings on top of the ordering. So first the featured jobs, and then all jobs ordered by the custom post field ‘order’ ascending. How can I achieve this?
    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Deric (a11n)

    (@dericleeyy)

    You could use this code snippet to define your custom field as the default sorting field:

    function change_listing_args( $query_args ) {
    	if ( ! empty( $query_args['orderby']['menu_order'] ) ) {
    
    		$query_args['meta_key'] = '<your meta key name here>';
    		$query_args['orderby']  = array_merge(
    			array_splice( $query_args['orderby'], 0, 1 ),
    			[ 'meta_value_num' => 'ASC' ], // This is assuming that the custom meta is numeric and smaller numbers come first.
    			$query_args['orderby']
    		);
    
    	}
    
    	return $query_args;
    }
    
    add_filter( 'get_job_listings_query_args', 'change_listing_args' );

    You can either apply this code in your theme’s function.php or in this plugin https://www.ads-software.com/plugins/code-snippets/.

    Thread Starter waffeltje

    (@waffeltje)

    Thanks a lot for your reply Deric, this is very helpfull.

    Can I also make sure that Featured Listings remain on top, and after all featured listings have been listed I sort on the custom meta key? Featured listings have a higher priority, so that’s why I want to keep them on top.

    Thanks in advance.

    Plugin Support Deric (a11n)

    (@dericleeyy)

    Featured listings should still be on top. At least, that’s my understanding from my discussion with the devs. Did you manage to give the code a try?

    Plugin Support Jay

    (@bluejay77)

    Hi there,

    It has been a while since we have heard from you, so I’m marking this topic as resolved.

    But if you have any further questions or need some more help, you’re welcome to reply here or open another thread.

    Thread Starter waffeltje

    (@waffeltje)

    Hi,

    Excuse me for the late reply. Indeed tested the code and everything is working as expected; featured listings are sorted before unfeatured listings with taking the custom post meta field into account.

    Thanks for you help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Ordering [jobs] based on custom post field’ is closed to new replies.