简体   繁体   English

无法正确连接到wp查询

[英]Trouble hooking into the right wp-query

I am 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. 我试图通过我的子主题的functions.php文件连接到Cherry Team Members插件 ,并将orderbymeta-keyorder为所需的内容。

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. 因此,我希望能够加入该查询并使用pre_get_posts操作进行修改,以便将来的插件更新不会影响此更改。

Below is what I've tried in my child theme's functions.php, but I think I am selecting the wrong query, as this code breaks my site. 以下是我在子主题的functions.php中尝试过的内容,但是我认为我选择了错误的查询,因为此代码破坏了我的网站。

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 anyone have any suggestions on how to select only the query that happens when I return the [cherry-team] shortcode? 有谁对如何仅选择返回[cherry-team]简码时发生的查询有任何建议?

You may just need to remove the $query->is_main_query() . 您可能只需要删除$query->is_main_query() The "main query" is set when WordPress decides what to query for the current Request URI (a certain page, post, category, etc.) - and since this is returned via a shortcode, I'd wager that $query->is_main_query() is returning false. 当WordPress确定要查询当前请求URI(特定页面,帖子,类别等)的内容时,将设置“主要查询”,并且由于这是通过短代码返回的,因此我押注$query->is_main_query()返回false。

Also, you're attempting to assign the post type in your if statement: 另外,您尝试在if语句中分配帖子类型:

$query->query_vars['post_type'] = 'team' 

should instead use a comparison operator: 相反,应使用比较运算符

$query->query_vars['post_type'] == 'team' 

try to create your function in a separate php file, and include that php file into the plugin main file. 尝试在单独的php文件中创建函数,并将该php文件包含到插件主文件中。 keep a backup of this file. 保留此文件的备份。 So, if your plugin is updated, you only have to add one line of code into the main plugin file to load your custom coded function. 因此,如果您的插件已更新,则只需在主插件文件中添加一行代码即可加载您的自定义编码函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM