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?
- How to print the excuted sql right after its execution
- Whats the difference between post_limits and pre_get_posts?
- Make a WP Query search match exactly the search term
- Query WooCommerce orders where meta data does not exist
- Show only oldest post by author
- How to get the posts published in last two days using WP_Query?
- Dynamically Override Fancy Title – Part II
- Transient pagination not working properly
- Can not switch the queried post in pre_get_posts hook
- List the 5 most recent child pages
- SELECT * FROM $wpdb->posts WHERE ID > 160
- What is considered a default query for the request filter?
- $wpdb insert is not work
- How WordPress attaches its Featured Images with posts?
- How to find exact match for search term in WP_Query? What is the additional string added in LIKE query in WP_Query?
- wpdb custom post_type problem
- How to query a custom post type with a taxonomy filter but display post type archive page?
- Any possible way to make $wpdb->get_results() return anything else than array?
- Change query_posts to WP_Query in page but does not work
- Set conditional on template based on referring page slug
- Woocommerce – Changing the order of the upsell products [closed]
- How to get a query variable?
- date_query seems to be ignored by wp_query
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Sort WordPress Posts Meta value by Week not Day
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- Why Query is returning empty array?
- WP User Query with Custom Fields and Search Results
- meta_value timestamp older than now
- Clear Terms from Taxonomy for Specific Post IDs?
- How to filter query loop block with a search string from the query parameters
- Function to retrieve IDs of posts, cache results, and improve wp_query
- How to avoid filling up an array each time I run a WP_query?
- What is the equivalent WP_Query of a SQL Query?
- How to get_comments() ordered by date and parent?
- Merge two queries and remove duplicate
- Fetch only categorized posts
- Custom pagination (Title, date and teaser)
- Fire query on ajax post url page
- Wpdb generates too many queries
- Showing posts from 4 categories along with all latest posts
- how to add limit records in wordpress query
- WP_Query by meta key not returning any posts
- SQL query into to WP query
- Search results stuck on page 1