Skip to content

Instantly share code, notes, and snippets.

View opicron's full-sized avatar

Robbert Langezaal opicron

View GitHub Profile
@opicron
opicron / functions.php
Last active October 20, 2022 10:37
searchwp custom code #php #searchwp
function my_searchwp_pre_search_terms( $terms, $engine ) {
$exact_match = get_posts( array(
'post_type' => 'product',
'nopaging' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $terms,
@opicron
opicron / woocommerce_sku_to_id.php
Last active October 20, 2022 10:37
change sku to id for woocommerce product table #php #woocommerce
<?php
function my_get_product_id_by_sku( $sku = false ) {
global $wpdb;
if( !$sku )
return null;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
@opicron
opicron / functions.php
Last active October 20, 2022 10:39 — forked from Jon007/functions.php
Add default Product Attributes to all WooCommerce products #php #woocommerce
/*
* Example adds certain named Product Attribute fields to the product Edit screen ready for completion.
* (Pre-requisite )
* This saves the Shop Admin having to add them manually.
*
* Why might you want to do this instead of adding Custom fields? There's plenty of nice documentation on adding custom fields
* for example: http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
*
* Well a Product Attributes are a built in WooCommerce feature, using Terms which are a built in Wordpress feature.
* - no add-ons required
@opicron
opicron / test.php
Last active October 20, 2022 10:39
SearchWP custom search grouped/child #php #searchwp
<?php
//Exclude child products from searches
function my_searchwp_exclude( $ids, $engine, $terms ){
$excluded_product_ids = get_posts( array(
'post_type' => 'product',
'nopaging' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_children',
@opicron
opicron / filter-search-results.php
Last active October 20, 2022 10:40
filter search results #php #searchwp
<?php
function filter_the_posts($posts, &$wp_query) {
if( is_search() )
{
//make array of product ids
$ids = array();
foreach ($posts as $key => $post)
{
$ids[$post->ID] = $post->ID;