Hooking into the right wp-query

Hey there, this may be beyond the scope of support here, but I’m trying to hook into the Cherry Team Members plugin via my child theme’s functions.php file and modify the orderby, meta-key, and order to what I want.

I’ve already successfully accomplished my end goal by editing the plugin files themselves as outlined here by modifying this file specifically, but I know that if I update the plugin, my changes will be lost. Therefore, I would like to be able to hook into this query and make my modifications using the pre_get_posts action so that future plugin updates will not affect this change.

Below is what I’ve tried in my child theme’s functions.php, but I think I am selecting the wrong query on line 5, as this code breaks my site.

add_action( 'pre_get_posts', 'modify_team_query' );
function modify_team_query( $query ) {

    // Check if on frontend and main query is modified
    if( ! is_admin() && $query->is_main_query() && $query->query_vars['post_type'] = 'team' ) {

        $query->set('meta_key', 'last_name');
        $query->set('orderby','meta_value title');
        $query->set('order', 'ASC');
    }
}

Does you have any suggestions on how to select only the query that happens when I return the [cherry-team] shortcode?

Nevermind. I got it. I didn’t need the $query->is_main_query() and I accidentally used = instead of == in my code. The proper code should be:

add_action( 'pre_get_posts', 'modify_team_query' );
function modify_team_query( $query ) {

	// Check if on frontend and query post_type is 'team'
	if( ! is_admin() && $query->query_vars['post_type'] == 'team' ) {
 
		$query->set('meta_key', 'last_name');
		$query->set('orderby','meta_value title');
 		$query->set('order', 'DESC');
	}
}

Hi @bobbybosler,

Glad it’s working now and thanks for sharing :wink:

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.