spacewood 0 ارسال شده در شهریور 99 گزارش بازنشر ارسال شده در شهریور 99 (ویرایش شده) در کلاس PHP متغیر $where در تابع function posts_where($where) در هنگام اجرای افرونه چگونه مقدار لازم را دریافت می کند؟ و به چه مقداری نیاز دارد؟ <?php /* Plugin Name: Search by Post ID Description: Enables the user to search by Post ID using the built-in search in all website. Works for all kinds of posts (posts, pages, custom post types and media). Version: 1.0 Author: Marcos Rezende Author URI: https://www.linkedin.com/in/rezehnde/ */ new RezehndeSearchById(); class RezehndeSearchById { function __construct() { add_filter('posts_where', array($this, 'posts_where')); } function posts_where($where) { if(is_search()) { $s = $_GET['s']; if(!empty($s)) { if(is_numeric($s)) { global $wpdb; $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID = ' . $s . ') OR (' . $wpdb->posts . '.post_title LIKE', $where); } elseif(preg_match("/^(\d+)(,\s*\d+)*\$/", $s)) // string of post IDs { global $wpdb; $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID in (' . $s . ')) OR (' . $wpdb->posts . '.post_title LIKE', $where); } } } return $where; } } ?> ویرایش شده شهریور 99 توسط spacewood نقل قول لینک به ارسال
yazdaniwp 1187 ارسال شده در شهریور 99 گزارش بازنشر ارسال شده در شهریور 99 در 1 ساعت قبل، spacewood گفته است : در کلاس PHP متغیر $where در تابع function posts_where($where) در هنگام اجرای افرونه چگونه مقدار لازم را دریافت می کند؟ و به چه مقداری نیاز دارد؟ <?php /* Plugin Name: Search by Post ID Description: Enables the user to search by Post ID using the built-in search in all website. Works for all kinds of posts (posts, pages, custom post types and media). Version: 1.0 Author: Marcos Rezende Author URI: https://www.linkedin.com/in/rezehnde/ */ new RezehndeSearchById(); class RezehndeSearchById { function __construct() { add_filter('posts_where', array($this, 'posts_where')); } function posts_where($where) { if(is_search()) { $s = $_GET['s']; if(!empty($s)) { if(is_numeric($s)) { global $wpdb; $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID = ' . $s . ') OR (' . $wpdb->posts . '.post_title LIKE', $where); } elseif(preg_match("/^(\d+)(,\s*\d+)*\$/", $s)) // string of post IDs { global $wpdb; $where = str_replace('(' . $wpdb->posts . '.post_title LIKE', '(' . $wpdb->posts . '.ID in (' . $s . ')) OR (' . $wpdb->posts . '.post_title LIKE', $where); } } } return $where; } } ?> کلا پارامتر $where برای شرطی هست که در کوئری اعمال میشه برای واکشی رکوردها. ساختار کوئری ای که برای دریافت اطلاعات از دیتابیس استفاده میشه چیزی شبیه به مورد زیر هست: SELECT * FROM wp_posts WHERE ID = 15; حالا این فانکشنی که دادید یا باید یه شناسه یا همون post_is بهش بدید یا چند تا آیدی که با , از هم جدا شدن. 1 نقل قول لینک به ارسال
پست های پیشنهاد شده
به گفتگو بپیوندید
هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .