Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Declaring a var, placing it in a query and using the output of the query?

First, this is confusing:

$result   = $newdb->get_results("SELECT ".$column1.",".$column2."  FROM members");  
$columnq  = $newdb->get_results("SELECT ".$column1." FROM members");
$columnq2 = $newdb->get_results("SELECT ".$column2." FROM members");

You should only need the first of those or the last two (with one alteration).

Secondly, get_results will return an array or an object (default), so using it as you do here isn’t going to work:

'post_title'   => $columnq,

post_title does not accept an array or an object as a value.

If you want to retrieve a single string value from the database use get_var. Then this will work:

$columnq  = $newdb->get_row("SELECT ".$column1." FROM members");
$postargs = array(
        'post_title'   => $columnq,
        'post_status'  => 'draft',
        'post_type'    => 'bedrijf'
);

To retrieve a single row use get_row and with proper syntax:

$result   = $newdb->get_results("SELECT ".$column1.",".$column2."  FROM members");  
$postargs = array(
        'post_title'   => $result[$column1],
        'post_status'  => 'draft',
        'post_type'    => 'bedrijf'
);

But your code is still a bit confusing as you appear to be retrieving all rows from your external database/table but trying to use those rows as if they were a single row.

$columnq  = $newdb->get_results("SELECT {$column1},{$column2} FROM members");
foreach ($columnq as $c) {
  $postargs = array(
      'post_title'   => $c->column1,
      'post_status'  => 'draft',
      'post_type'    => 'bedrijf'
  );
  // code to insert post
}

Related Posts:

  1. Using WordPress public query variables
  2. How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
  3. Search custom post type by meta data
  4. How to display liked posts of current user in wordpress?
  5. Filter query posts by the count of metas for a meta key
  6. How to tell if $query_var isset?
  7. Save default value for empty or missing post meta
  8. Grouping related postmeta data via SQL query
  9. mySQL query. ORDER BY meta_key
  10. Remove posts from query for events whose start date has passed
  11. Query metas (and not : query posts by metas)
  12. MySQL variable in query
  13. Issue with using add_rewrite_rule() for post querying custom fields
  14. Query posts according to specific post meta values
  15. Passing parameters to a static front page
  16. Order by meta value pro first then meta value free in my search function
  17. get_post_meta with WP_query
  18. Combining two meta_values within one row with query
  19. Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
  20. Sort by postmeta on when searching
  21. Not getting input from get_query_var
  22. Use $wpdb->get_results with filter based on array
  23. What is the best way to query posts based on live data?
  24. Custom MySQL Query for Post and Post Meta
  25. How to display SQL query that ran in query?
  26. How can I create a meta_query with an array as meta_field?
  27. Query multiple meta key values?
  28. How to Optimize WP site for millions of posts
  29. order by numeric value for meta value
  30. How to get comments by post ID?
  31. Differences between wpdb->get_results() and wpdb->query()
  32. Is there a way of increasing the speed of this query?
  33. Get all image from single page using this query
  34. How can I query all users who registered today?
  35. Get Terms by IDs with IDs order
  36. Reversing the order of posts AFTER the query is performed
  37. what are the numbers between curly brackets in search query
  38. simple sql query on wp_postmeta very slow
  39. Is there a way to extend WP_query so Custom Post Types can have properties?
  40. When/why does ‘$query->get( ‘tax_query’ );’ return empty?
  41. How to get link and title of next and previous post on single page
  42. Create pagination and order according to alphabet
  43. wpdb get posts by taxonomy SQL
  44. How to List Events by Year and Month Using Advanced Custom Fields?
  45. What is the most efficient way of querying posts based on visits and date for current day?
  46. Is there any difference between hooks posts_where with posts_join and posts_search performance wise?
  47. wordpress query – orderby child post date
  48. How many WordPress SQL Queries per page?
  49. How to display Section for certain time
  50. Add multiple value to a query variable in WordPress
  51. Custom query_var causes displaying posts archive on front page
  52. Slow wp_enqueue_media()
  53. Multipart/formatted MySQL query problem
  54. Custom Queries: Joining On Meta Values From Two Custom Post Types
  55. What is the most efficient way of implementing a favorite post system?
  56. Query Custom Meta Value with Increment
  57. Remove [gallery] shortcode altogether
  58. Compare two numeric custom fields
  59. wp_dropdown_categories with multiple select
  60. Advanced Custom Fields – Query Efficiency
  61. Why does get_the_time(‘F j’) return November 30 for all posts?
  62. Query by one meta_key and sort by another (possibly NULL value)
  63. How to tune search argument in WP_Query to show only exactly the same results?
  64. Custom query filter not working on woocommerce category page
  65. How to delete a transient on post/page publish?
  66. WPDB Multiple site’s posts and get featured images
  67. Very slow query
  68. query multiple taxonomy and show post count
  69. Search Terms – Querying on either description__like OR name__like in the same Term Query?
  70. Query & Sort Comments by custom comment meta
  71. How to get my loop to pull posts into three columns
  72. How to display lastest post date in the homepage?
  73. Custom $wpdb Query for Custom Post Type by Category
  74. Get a user’s most recent post title
  75. Categories and products in random order
  76. add_query_arg() XSS Vulnerability
  77. Count user posts by user ID, Post type and Post status
  78. Can’t pass table to $wpdb->prepare
  79. Query set using tax_query with relation ‘OR’ modifies category object?
  80. $query->query_var[‘post_type’] not set for pages
  81. is_archive() doesn’t work on public query var archive pages?
  82. How to extract all ID variables from a query string?
  83. When add_query_arg() is necessary?
  84. Different Main Navigation per category
  85. How to get category link without a database query
  86. Insert html after certain amount of posts?
  87. wp remove query
  88. Show recent products first but “sold out last” in query
  89. WordPress creating excessive joins on meta_query with search
  90. Show All Posts Insert Edit Link
  91. $wpdb->get_var not returning a result
  92. search through post-type attachments titles
  93. How to count get_users query?
  94. Date query year and month OR just year
  95. cron job to auto delete posts of a specific post type older than x days
  96. Possible to get posts from multiple meta keys/values in a single query?
  97. Are database queries created using WordPress filters protected from SQL injection?
  98. Get IDs of posts currently visible on archive
  99. Get posts by category with pure SQL query
  100. can’t get query to order posts by acf datepicker
Categories query Tags post-meta, query, variables
Customize Option Framework
Stop `update_post_meta` from working each time page refreshes?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress