if you are trying to query from the same database in which your WordPress is installed then there is a great way to do sql query with ease. WordPress has a database access abstraction class called wpdb
that allows you to run raw mysql query without any issue.
You can start using it like one of two methods bellow:
// 1st Method - Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );
or
// 2nd Method - Utilizing the $GLOBALS superglobal. Does not require global keyword ( but may not be best practice )
$results = $GLOBALS['wpdb']->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );
Explore the documentation here and get started!
Related Posts:
- How to display SQL query that ran in query?
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Multipart/formatted MySQL query problem
- WordPress creating excessive joins on meta_query with search
- cron job to auto delete posts of a specific post type older than x days
- Are database queries created using WordPress filters protected from SQL injection?
- Get posts by category with pure SQL query
- Custom query to get terms from post ids
- Get the timout value of a saved transient?
- $wpdb->query() vs. $wpdb->get_results() vs. phpMyAdmin
- Update slug (URL) of pending posts via phpMyAdmin
- What’s wrong with my $wpdb prepare?
- How do I see the mysql query generated by get_posts( $args )?
- sql select query in wordpress ‘page’ [closed]
- problem with sql query in wordpress plugin
- Attempt to improve WP search, can someone check my SQL query?
- wpdb query problem to access previous 3 days posts
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Custom Query – Based on user input
- how to insert missing tags into the posts through mySQL?
- $wpdb->prepare affecting the query?
- WordPress SQL search, how to handle SQL Injection?
- exclude pingbacks from wordpress SQL query
- prepare function sql safe method
- WordPress SQL query to tag all posts containing a specific word on title
- Select column name dynamically mySQL Query
- How do I query for posts by custom meta and those that have been stickied?
- Change pure SQL database query to WordPress post query?
- Cannot get sql request from Query object?
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- SQL to Query the db and return all posts and it’s metas
- SQL query to delete users with multiple meta keys and comments
- Nested select statements not working
- Speed up search query that searches in post meta?
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- Fastest and most efficient SQL query to check if UID exists
- How to make MySQL search queries with quotes
- How i make a custom sql query for Woocommerce
- $wpdb query for price in custom field value
- Run an update query in a function
- How to display a specific category using a custom Query in WordPress?
- Slow query when joining wp_posts with a lookup-table?
- Wpdb generates too many queries
- learn to run wpdb class
- SQL Query : how copy all tags of post into their post content in wordpress by sql query
- Where can I find the SQL to get the most used information by wordpress database?
- Custom Query for wp_posts using wp_postmeta
- 3 queries to update WordPress
- Differences between wpdb->get_results() and wpdb->query()
- Using WordPress public query variables
- Is there any difference between hooks posts_where with posts_join and posts_search performance wise?
- $query->query_var[‘post_type’] not set for pages
- Show recent products first but “sold out last” in query
- Get IDs of posts currently visible on archive
- WordPress query by category, sorted by custom field
- How many queries are normal to execute on a WP site?
- How to optimize my query filtering out unwanted data?
- Remove posts from query for events whose start date has passed
- Insert static element only once in query archive
- How to run a mysql query when admin updates user role?
- Error when using setup_postdata()
- pre_get_posts : ‘post__not_in’ doesn’t work with global variable
- Getting post data from private page
- Optional Meta Query
- SQL query to select posts from multiple categories
- Select Query demand excesive resources [closed]
- Create Indexes for Slow Queries
- Query to get number of posts last week
- I want to create a filter for the query string is this possible?
- Exclude categories from the_category();
- How can i show all categories using wp query?
- Query returning same results even though the ID changes
- Why my query ‘REPLACE INTO…’ does not work?
- Is it possible to query from external database? [duplicate]
- Restrict query to last day with posts
- Custom query object with Simple-Fields custom date field
- How to read the value of a WordPress $query associative array (hash) key
- PHP -> SQL Query with Summing
- Query posts based on previous query
- Waypoints + infinite scroll with custom query
- Customizing the_tags output?
- Querying with WP query using meta key price
- Re-sort get_posts query results
- How do I get all results from my query
- Pagination not working on custom post types with rewrite slug
- Filter orders by modify date
- How to add url parameter to every search query in SearchWp?
- Update WooCommerce stock status with SQL query if stock value is greater than 0
- SQL query to list all posts of specific post type with a specific set of associated postmeta values as a column
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- Category_in return empty
- How to run select query of post with category and tags?
- WordPress character set issue
- modifying main query to query for ranges e.g. from 1999 to 2016
- Querying users by meta value and getting a strange answer
- $wpdb: how to use a query as a base for another query?
- Multilingual WordPress page – using URL to determine language?
- WP_Query with checkbox meta_query – WordPress [duplicate]
- What is the best way to cache pages and queries in WordPress?