web_development:wordpress:wpdb

WPDB

global $wpdb;
$query = $wpdb->prepare( 
  "SELECT * FROM {$wpdb->posts} WHERE post_type = %s", 
  $post->post_type 
);
$results = $wpdb->get_results( $query );
global $wpdb;
$post = get_post();
$query = $wpdb->prepare(
	"SELECT * FROM {$wpdb->posts} WHERE post_type LIKE %s",
	'%' . like_escape( $post->post_type ) . '%'
);
$results = $wpdb->get_results( $query );
<?php 
global $wpdb;
$wpdb->insert(
	$wpdb->postmeta,
	array(
		'post_id'    => get_the_ID(),
		'meta_key'   => 'name',
		'meta_value' => 'John'
	),
	array( '%d', '%s', '%s' )
);
<?php
global $wpdb;
$wpdb->update(
	$wpdb->posts,
	array( 'post_type' => 'new' ),
	array( 'post_type' => 'old' ),
	array( '%s' ),
	array( '%s' )
);
<?php
global $wpdb;
$wpdb->delete( 
    $wpdb->postmeta, 
    array( 'meta_id' => 101 ), 
    array( '%d' ) 
);
  • web_development/wordpress/wpdb.txt
  • Last modified: 2020/10/09 04:10
  • by jimboobrien