Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
twentyfortysix / getAndCacheJson.php
Created July 31, 2018 21:35
WP - get and cache json
function get_json($url,$id){
$_url = $url.$id;
$transient = get_transient( $_url );
if(!empty($transient)){
return $transient;
}else{
$request = wp_remote_get( $_url );
if( is_wp_error( $request ) ) {
return false; // Bail early
for POST in $(wp post list --post_type=locations --meta_key=page_subtype --meta_value=event --format=ids); do wp post update $POST --post_status=draft; done
for POST in $(wp post list --post_type=post --category_name=tiskovky --format=ids); do wp post update $POST --post_type=tiskovky; done
@twentyfortysix
twentyfortysix / divide-loop.twig
Created January 27, 2020 18:11
divide for loop by n
{% for col in posts|batch((posts|length / 2)|round(0, 'ceil')) %}
<div class="col-12 col-sm-6">
{% for item in col %}
<a href="{{ item.link }}">
{{ item.title }}
</a>
{% endfor %}
</div>
{% endfor %}
<?php
function replacer($post_type, $field, $find, $replace, $wet){
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'post_status' => 'any'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
while ( $the_query->have_posts() ) {
<script>
jQuery(document).ready(function($) {
function findCommonElement(filters,classes) {
var result = true;
for(let i = 0; i < filters.length; i++) {
if(!classes.includes(filters[i])){
result = false;
break;
}
}
@twentyfortysix
twentyfortysix / importer.php
Created August 26, 2020 17:22
import post images to acf gallery
<?php
// field id must be the ACF ID
function post_images_to_ACF_gallery($post_type, $field_id){
$args = array(
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => -1,
'fields' => 'ids'
);
$the_query = new WP_Query( $args );
{{ constant('Twig_Environment::VERSION') }}
@twentyfortysix
twentyfortysix / recently_commented_posts.php
Created February 15, 2021 23:22
php, enclosure, timber, comments
<?php
$args = array(
'status' => 'approve',
'number' => 30,
'order' => 'DESC'
);
$comments = get_comments($args);
$post_ids = array_map(function($key, $value) {
return $value->comment_post_ID;
}, array_keys($comments), $comments);
<?php
// 1, 2, asc
usort($mix, function ($item1, $item2) {
return $item1->post_date <=> $item2->post_date;
});