Skip to content

Instantly share code, notes, and snippets.

@platoosom
platoosom / gist:8043b4c70f2434911f647ad3ea7a68ca
Created April 30, 2020 07:16
Wordpress query not include current post
$termlists = array();
$terms = get_the_terms( get_the_ID(), 'podcast-tag' );
if ( $terms && is_array( $terms ) ) {
foreach ( $terms as $term ) {
$termlists[] = $term->slug;
}
}
$query_args = array(
@platoosom
platoosom / gist:7f80093e58523b11028695396073a720
Created April 30, 2020 07:14
Wordpress get custom post by custom tags.
$terms = get_the_terms( get_the_ID(), 'podcast-tag' );
if ( $terms && is_array( $terms ) ) {
foreach ( $terms as $term ) {
$termlists[] = $term->slug;
}
}
$query_args = array(
'post_type' => 'podcast',
@platoosom
platoosom / gist:22337e15977639beca0ed981687d3f4c
Created April 20, 2020 04:29
Wordpress how to create widget
<?php
/**
* Register and load the widget
*/
add_action( 'widgets_init', 'topgear_load_widgets' );
function topgear_load_widgets() {
register_widget( 'Dream_Car_Calculator_Widget' );
}
@platoosom
platoosom / gist:71008f635392fd7281581ad852fedf0f
Last active April 17, 2020 08:20
Wordpress insert data into custom database
/**
* Insert LinkUrl to wp_imported_articles table
*
* @param $LinkUrl
* @return int
*/
function topgear_save_LinkUrl_todatabase($LinkUrl)
{
global $wpdb;
@platoosom
platoosom / gist:a7c2544a4930c4a06ba384e4c76936df
Created April 17, 2020 08:14
wordpress query data from custom table
/**
* Check post by postmeta
* @param $LinkUrl
* @return int
*/
function topgear_post_exists_by_LinkUrl($LinkUrl)
{
global $wpdb;
$wpdb->get_results("SELECT ID FROM `{$wpdb->base_prefix}imported_articles` WHERE LinkUrl = '".$LinkUrl."' ");
@platoosom
platoosom / gist:2d5c4f1d7aedf5f08ab05c1a70f84a41
Created April 17, 2020 08:05
Wordpress สร้างฐานข้อมูลขึ้นมาใหม่ถ้าหากมันไม่มี
/**
* Create custom table to keep import article url
*/
add_action('init', 'topgear_create_import_exists_table');
function topgear_create_import_exists_table()
{
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
@platoosom
platoosom / gist:b9913a8ff8ca00bb93726ab435a05b7f
Created April 17, 2020 04:38
wpml insert post in english language ให้โฟกัสที่บรรทัด 105-117 มันใช้ hook wpml_set_element_language_details ในการยัดภาษาเข้าไป ถ้าไม่มี hook นี้เวลาสร้างโพสต์มันจะเซฟเป็นภาษา default
function topgear_midnight_cron_task()
{
$action = isset($_GET['action'])? $_GET['action']: null;
if($action !== 'cron'){ return ;}
// Find Author
$user = get_user_by( 'login', 'Topgear' );
$userID = ($user->ID)? $user->ID : 4;
// Find Category
@platoosom
platoosom / gist:742e7fff640b36a68a2ee6f9f7a01975
Created April 17, 2020 03:25
wordpress check plugin activated เช็กว่าปลักอินเป้าหมาย activated หรือไม่
if ( is_plugin_active('sitepress-multilingual-cms/sitepress.php') ) {
include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
$_POST['icl_post_language'] = $language_code = 'en'; // change the language code
wpml_add_translatable_content( 'post_my_post_type', $ID, $language_code );
}
@platoosom
platoosom / gist:36bec5ece165317b6b3d55663b344148
Created April 16, 2020 08:28
Wordpress insert new post with thumbnail เวิร์ดเพรส ดึงรายการโพสต์มาจาก json จากนั้นสร้างโพสต์ใหม่และใส่ thumbnail เข้าไปด้วย โค้ดนี้ไว้ลอกการดูดภาพมาทำ thumbnail และการสร้างโพสต์ด้วยโค้ด
$action = isset($_GET['action'])? $_GET['action']: null;
if($action !== 'cron'){ return ;}
// Find Author
$user = get_user_by( 'login', 'Topgear' );
$userID = ($user->ID)? $user->ID : 4;
// Find Category
$category = get_category_by_slug( 'car-news' );
$categoryID = ($category->term_id)? $category->term_id: null;
@platoosom
platoosom / gist:483d318410bc57b227dc11d9f9c589c1
Created April 14, 2020 06:00
Wordpress get user by role
<div class="container mt-100">
<h1 class="title text-center mb-40">Featured Guests</h1>
<div class="row">
<div class="col-sm-4 col-6">
<div class="item-advisory">
<?php
$args = array(
'role' => 'featured_guest',
'orderby' => 'user_nicename',
'order' => 'ASC'