Skip to content

Instantly share code, notes, and snippets.

View felipeelia's full-sized avatar

Felipe Elia felipeelia

View GitHub Profile
@felipeelia
felipeelia / add_rand_orderby_restapi_post.php
Created March 29, 2019 12:14
Randomize the content list in REST API passing `orderby=rand` as parameter.
<?php
/**
* Plugin Name: REST API - Post list randomize
* Description: Randomize the content list in REST API passing `orderby=rand` as parameter.
* Version: 1.0.0
* Author: Felipe Elia | Codeable
* Author URI: https://codeable.io/developers/felipe-elia?ref=qGTOJ
*/
/**
@felipeelia
felipeelia / elasticpress-remove-some-meta.php
Last active April 6, 2022 19:33
Remove some meta fields from the ElasticPress/Elasticsearch index
<?php
add_filter(
'ep_prepare_meta_data',
function ( $prepared_meta ) {
foreach ( $prepared_meta as $key => $meta_val ) {
// Remove empty meta fields.
if ( empty( $key ) || empty( $meta_val ) || empty( $meta_val[0] ) ) {
unset( $prepared_meta[ $key ] );
continue;
}
@felipeelia
felipeelia / paginacao-wordpress.php
Created August 4, 2019 13:17
Paginação no WordPress
<?php
/**
* Função para exibir paginação
*
* @param WP_Query $query Query que deverá ser paginada.
* @param integer $range Quantidade de páginas ao redor da página atual.
* @param string $get_param Nome do parâmetro em `$_GET` com o número da página atual.
* @return string HTML com a paginação
*/
function starter_pagination( $query = null, $range = 2, $get_param = null ) {
@felipeelia
felipeelia / elasticpress-search-thumbnail-exists.php
Created March 24, 2022 11:26
Change the ElasticPress search query to match only content with a thumbnail set.
<?php
/**
* Since ElasticPress 4.0, thumbnails are indexed in a separate field.
*/
add_filter(
'ep_post_formatted_args',
function ( $formatted_args ) {
if ( empty( $_GET['has_image'] ) ) {
return $formatted_args;
}
@felipeelia
felipeelia / elasticpress-dynamic-sync-debug.php
Created March 6, 2022 19:29
Set of functions to help debugging ElasticPress dynamic sync requests
<?php
/**
* This is a set of functions that can be used to debug
* the dynamic sync made by ElasticPress
*
* phpcs:disable WordPress.PHP.DevelopmentFunctions -- Reason: the whole purpose of the file is to use error_log().
*
* @package ElasticPress_Custom
*/
@felipeelia
felipeelia / elasticpress-partial-matches-on-woocommerce-skus.php
Created February 17, 2022 13:31
Add to ElasticPress the capability to partially match a WooCommerce SKU.
<?php
/**
* Add the ngram analyzer if not added yet by autosuggest.
*
* @param array $mapping The mapping array.
* @return array
*/
function ep_custom_add_edge_ngram_analyzer( $mapping ) {
if ( ! @isset( $mapping['settings']['analysis']['analyzer']['edge_ngram_analyzer'] ) ) {
$mapping['settings']['analysis']['analyzer']['edge_ngram_analyzer'] = array(
@felipeelia
felipeelia / elasticpress-woocommerce-customer-order-csv-export.php
Created February 3, 2022 19:31
Add compatibility between ElasticPress and WooCommerce Customer/Order/Coupon Export
<?php
/**
* Add compatibility between ElasticPress and WooCommerce Customer/Order/Coupon Export.
*/
add_filter(
'ep_sync_taxonomies',
function ( $taxonomies ) {
if ( class_exists( '\SkyVerge\WooCommerce\CSV_Export\Taxonomies_Handler' ) ) {
$taxonomies[] = \SkyVerge\WooCommerce\CSV_Export\Taxonomies_Handler::TAXONOMY_NAME_ORDERS;
@felipeelia
felipeelia / elasticpress-decay-adjustments.php
Created January 21, 2022 14:23
ElasticPress decay adjustments
<?php
/*
* Part of this comment in the ElasticPress repository
*
* @see https://github.com/10up/ElasticPress/issues/2073#issuecomment-776155310
*/
add_filter( 'epwr_boost_mode', function() { return 'multiply'; } );
add_filter( 'epwr_offset', function() { return '15d'; } );
add_filter( 'epwr_scale', function() { return '30d'; } );
add_filter( 'epwr_decay', function() { return 0.5; } );
@felipeelia
felipeelia / elasticpress-search-algorithm-4.0.php
Created December 10, 2021 19:28
ElasticPress 4.0.0 Search Algorithm
<?php
/**
* These functions can be used by ElasticPress 3.5.5+ users wanting to
* use the search algorithm introduced as default in EP 4.0
*/
/**
* Change the search algorithm to the one introduced in ElasticPress 4.0.
*
* @param array $query Current query
@felipeelia
felipeelia / ep-add-cross-fields.php
Last active December 10, 2021 19:25
ElasticPress: Make Elasticsearch search for the string in different fields
<?php
/**
* Add a cross_field type clause to the ES query, so it searches for terms
* in different fields.
*
* @param array $query Current query
* @param array $args Query variables
* @param string $search_text Search text
* @param array $search_fields Search fields