KHÓA HỌC DATABASE TRONG WORDPRESS
  • KHÓA HỌC DATABASE WORDPRESS
    • Giới thiệu khóa học
    • Khóa học này dành cho ai ?
    • Nội dung khóa học
    • Chuẩn bị cho khóa học
  • Bài 1: Phân tích thiết kế Database WordPress
    • Bài viết và chuyên mục liên kế như thế nào trong database ?
    • Hình ảnh được lưu ở đâu trong databse WordPress ?
    • Ảnh đại diện của một bài viết được lưu như thế nào?
    • Menu của WordPress được lưu ở đâu trong databse?
    • Widget được lưu ở dâu trong databse ?
    • Một plugin active lưu ở đâu?
  • Bài 2: Tạo bảng mới trong database WordPress
    • Tìm hiểu về Class wpdb & Tạo bảng mới
    • Tạo bảng mới với khóa ngoại
  • Bài 3: Thêm, sửa, xóa dữ liệu database WordPress vơi $wpdb
    • Thêm dữ liệu vào database WordPress
    • Cập nhật dữ liệu database WordPress
    • Xóa dữ liệu database WordPress
  • Bài 4: Query dữ liệu database Wordpress
    • Get results
    • Get row
    • Get var
    • Get col
  • Bài 5: Thực hành query dữ liệu database WordPress
    • Get 10 bài viết mới nhất
    • Truy vấn sql database wordpress get 10 bài viết mới nhất có hình đại diện
    • Truy vấn sql database wordpress get 10 bài viết theo chuyên mục
  • Bài 6: Thực hành tạo form contact với custom databse
    • Download template form contact & Tạo database lưu trữ dữ liệu contact
    • Xử lý dữ liệu vào lưu dữ liệu vào database
    • Cấu hình SMTP & Gởi mail liên hệ thông qua hàm wp_email
    • Hiển thị dữ liệu contact và tính năng tìm kiếm
    • Tối ưu database và bảo mật
Powered by GitBook
On this page
  1. Bài 5: Thực hành query dữ liệu database WordPress

Truy vấn sql database wordpress get 10 bài viết theo chuyên mục

<?php
	global $wpdb;
	$table 	= $wpdb->prefix . 'posts';
	$tablemeta 	= $wpdb->prefix . 'postmeta';
	$tableRelationships = $wpdb->prefix . 'term_relationships';
	$offset 	= 0;
	$limit 	  = 10;
	$sql = "SELECT  `post`.`ID`,
		`post`.`post_author` as author,
		`post`.`post_date` as date,
		`post`.`post_title` as title,
		`post`.`post_name` as slug,
		`thumb_link`.`meta_value` as image
	FROM $table as post
	LEFT JOIN $tablemeta as thumb_id ON `thumb_id`.`post_id` = `post`.`ID` AND `thumb_id`.`meta_key` = '_thumbnail_id'
	LEFT JOIN $tablemeta as thumb_link ON `thumb_link`.`post_id` = `thumb_id`.`meta_value` AND `thumb_link`.`meta_key` = '_wp_attached_file'
	LEFT JOIN $tableRelationships as relation ON `relation`.`object_id` = `post`.`ID`
	WHERE 	`post`.`post_type` = 'post' AND
		`post`.`post_status` = 'publish' AND
		`relation`.`term_taxonomy_id` = %d
	GROUP BY `ID`
	ORDER BY `date` DESC
	LIMIT %d OFFSET %d";
	return $data = $wpdb->get_results( $wpdb->prepare($sql, $cat_id, $limit, $offset), ARRAY_A);
?>
PreviousTruy vấn sql database wordpress get 10 bài viết mới nhất có hình đại diệnNextBài 6: Thực hành tạo form contact với custom databse

Last updated 1 year ago