Just to clarify the get_var()
method of $wpdb
does work just fine in this context:
global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT ID FROM wp_posts WHERE post_name="hello-world"");
echo $helloworld_id;
Actually it is more practical in this context, because a single variable is returned, which is what is actually wanted.
Related Posts:
- How to make an activities stream mixing posts and comments?
- How to correctly pass values to wpdb->prepare()?
- Is it possible to query from external database? [duplicate]
- List of ways to access WordPress database?
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Include post_status check within $wpdb query
- wpdb get_results() returns only 2 rows
- Pagination with custom SQL query
- Return only Count from a wp_query request?
- Export wordpress table to excel
- What is an efficient way to query based on post_meta?
- SQL query equivalent to WP User Query
- how to retrieve specific product attribute value in an sql query?
- WP_Query Performance Issues with meta_query
- Should close $wpdb via $wpdb->close()
- Get the post permalink within the loop but without additional DB query
- Filter WordPress posts by between parameter
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- Select from wp_post and multiple meta_value from wp_postmeta
- Order by custom table and multiplication
- $wpdb select date range of posts
- List of the years with posts presented
- Optional Meta Query
- Custom Query num_rows returns wrong amount
- Editing the default wordpress search
- Iterating through $wpdb query without using get_results for large query results
- WP Query related posts by tags
- Order by summing multiple values
- wp_Qwery works to slow
- Include data from custom table in WP_Query
- No results found from a $wpdb->get_results() query when trying to join more than one meta key query
- How to access custom WordPress database tables
- How to pass orderby params to $wpdb->prepare()?
- Make a SQL query with wpdb in WordPress
- WordPress query posts with multiple post_meta data
- Specify strict ‘order by’ in WordPress query
- How to set up hierarchical relationships without using plugins / meta query
- Does WordPress have something like Drupal’s DB API?
- Query where ANDing slug values not working
- Slow Query On Search
- How to SQL query posts IDs by categories AND authors?
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- Passing a SQL query to the WP Query
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- How to get meta key list efficiently?
- How to get user avatar via WPDB
- Translating WP query into to SQL query
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- What argument does my function need to echo get_results() query results
- WP_Query always returning the last custom post
- Optimising amount of calls to custom fields
- wpdb LIKE request shows all database data
- Convert a WP Query into a simpler SQL query to fetch only COUNT of posts
- How much does $wpdb->prepare(), then $wpdb->query() VS straight $wpdb->query(), can slow down the load time of whole page
- how to use transient method?
- Optimizing AJAX Query with Large Database
- When should you use WP_Query vs query_posts() vs get_posts()?
- WP_Query with “post_title LIKE ‘something%'”?
- How to print the excuted sql right after its execution
- WPDB Insert or if exists Update
- order by numeric value for meta value
- get_results using wpdb
- Wp get all the sub pages of the parent using wp query
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- How to query for most viewed posts and show top 5
- WP_Query vs get_posts
- WP_Query by meta key not returning any posts
- Get posts that were most recently tagged
- SQL query to rewrite all media URLs to end with .webp
- SQL query into to WP query
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- wp_query order by rand is repeating posts
- Add custom argument to WP_Query and modify SQL where clause
- A Depth Like Parameter For “get_posts”
- get_post_meta bringing back results, but $wpdb->postmeta doesn’t
- Use meta query only 3 or more results?
- Search query alteration not working for meta values
- Search results stuck on page 1
- Group By query based on Custom Field
- Advanced Search – Is this possible?
- Change permalinks in posts via SQL
- Use value from meta key array for use in WP_Query
- Is there another way to retrieve a post_id from post_meta other than a SQL query?
- How to get posts that have certain meta key value and order based on another meta key’s value
- How to make OR condition in WP_Query
- How to use wp-query to search for posts where post_content OR post_title OR post_name
- Filtering custom post type list in admin by custom meta key/value
- How to use meta_query to retrieve posts from multiple custom post type
- Modify WordPress Search
- Block Editor – WordPress 6.1 – CPT Archive Issue – While the title changes in the loop, all records display the same data
- $wp_query->found_posts; returns zero
- Rewrite URL custom search query
- Multiple queries and pagination
- posts_per_page showing 16 elements instead of 3
- How to short circuit a wordpress query that is not related to posts?
- query loop “inherit query from template” prevents setting sort order
- WordPress DB query
- SQL errors when querying for something with apostrophes
- Function wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder
- WP_Query equivalent of SQL UNION ALL