رفتن به مطلب

نحوه عملکرد افزونه Search by Post ID


پست های پیشنهاد شده

در کلاس 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;
	}
}
?>

 

ویرایش شده توسط spacewood
لینک به ارسال
  • spacewood عنوان را به نحوه عملکرد افزونه Search by Post ID تغییر داده است
در 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 بهش بدید یا چند تا آیدی که با , از هم جدا شدن.

لینک به ارسال

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع ...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...