MySQL 如何使用 WordPress 进行自定义查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/969932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:26:45  来源:igfitidea点击:

How do I make custom query with WordPress?

wordpressmysqlwordpress-plugin

提问by inkedmn

I need to make a query in a WordPress plugin but I couldn't find a WordPress function and I'm not sure is right to use mysql_query

我需要在 WordPress 插件中进行查询,但我找不到 WordPress 功能,我不确定使用 mysql_query 是否正确

The functions I've found enable ordering and grouping but I need to use also join and in (list).

我发现的功能支持排序和分组,但我还需要使用 join 和 in (list)。

Is there a way?

有办法吗?

回答by inkedmn

It looks like you'd want to use the $wpdbclass (which has functions for directly accessing and manipulating the wordpress database). It lets you do things like:

看起来您想要使用$wpdb该类(它具有直接访问和操作 wordpress 数据库的功能)。它可以让您执行以下操作:

<?php $wpdb->query('select * from my_plugin_table where foo = "bar"'); ?>

Documentation here.

文档在这里

回答by DrLightman

To pull out rows maybe you'd use:

要拉出行,您可能会使用:

$myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );