how can we execute custom queries on wordpress

how can we execute custom queries on wordpress

We can use the custom queries in wordpress. Using the custom queries we can fetch the wordpress table data as per our custom requirement.

how can we execute custom queries on wordpress

We you create your table then you can fetch values from that table using following command.

$ourdata = $wpdb->( "SELECT id, name FROM yourtable" );

If you want to create the table in wordpress then use following article.

https://purabtech.in/create-add-tables-wordpress-theme/

More example about executing the own custom quries.

$wpdb->query("
	DELETE FROM $wpdb->postmeta WHERE post_id = '123'
	AND meta_key = 'google'");

another example about adding the data into table which is from wordpress:

$table_name = "your_table";
 $welcome_name = "Mr. WordPressapi";
  $welcome_text = "Congratulations, you just completed the installation!";

  $insert = "INSERT INTO " . $table_name .
            " (time, name, text) " .
            "VALUES ('" . time() . "','" . $wpdb->escape($welcome_name) . "','" . $wpdb->escape($welcome_text) . "')";

  $results = $wpdb->query( $insert );

Getting the single value from any table

$user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));

Get the single row value from wordpress table

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);
how can we execute custom queries on wordpress
how can we execute custom queries on wordpress

you can use inner join, outer join and multiple join in wordpress custom queries.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

One thought on “how can we execute custom queries on wordpress”

Leave a Reply

Your email address will not be published.