Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
jorpdesigns / exclude-page-from-search.php
Created July 14, 2021 10:32
Snippet to exclude pages from WordPress search results
<?php
add_action('pre_get_posts','exclude_pages_from_search');
function exclude_pages_from_search( $query ){
if( $query->is_main_query() && is_search() ){
$post_ids = array(123, 456, 345); // Replace with your own ids
$query->set('post__not_in', $post_ids);
}
}
@jorpdesigns
jorpdesigns / allow-numeric-urls.php
Created July 14, 2021 10:29
Snippet to allow numeric URLs in WordPress
<?php
add_filter( 'wp_unique_post_slug', 'numeric_post_slug', 10, 6 );
function numeric_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
global $wpdb;
if ( ! is_numeric( $original_slug ) || $slug === $original_slug ) {
return $slug;
}
@jorpdesigns
jorpdesigns / remove-archive-prefix.php
Created July 13, 2021 19:47
Snippet to remove prefixes from archive titles
<?php
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>' ;
} elseif ( is_tax() ) { //for custom post types
@jorpdesigns
jorpdesigns / custom-post-tax-query.php
Created July 13, 2021 19:46
Function for querying custom post type based on custom taxonomy
<?php
function custom_post_tax_query() {
$args = array(
'post_type' => 'custom-post-type', // Replace with custom post type
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'menu_order',
'tax_query' => array( array(
'taxonomy' => 'custom_taxonomy', // Replace with custom taxonomy
@jorpdesigns
jorpdesigns / sibling-pages-query.php
Created July 13, 2021 19:42
Function for querying sibling pages that share same parent
<?php
function sibling_pages_query() {
$parentPageObject = get_page_by_title( 'Parent Page' ); // Replace with parent page title
$output = '';
if ( $parentPageObject ) {
$subPagesArray = get_pages(
array(
'child_of' => $parentPageObject->ID,
@jorpdesigns
jorpdesigns / pages-query-acf.php
Last active July 13, 2021 19:40
Function for querying pages based on custom category added by ACF
<?php
function pages_query_acf() {
// Parameters available here: https://developer.wordpress.org/reference/classes/wp_query/
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'menu_order',
@jorpdesigns
jorpdesigns / taxonomy-terms-query.php
Created July 13, 2021 19:33
Function for querying taxonomy terms
<?php
function taxonomy_terms_query() {
// Parameters available here: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#parameters
$args = array(
'taxonomy' => 'product_cat',
'child_of' => 5, // Get child terms of category with ID 5
'parent' => 5, // Get direct-child terms of category with ID 5
);
@jorpdesigns
jorpdesigns / posts-query.php
Created July 13, 2021 19:31
Function for querying posts
<?php
function posts_query() {
// Parameters available here: https://developer.wordpress.org/reference/classes/wp_query/
$args = array(
'post_type' => 'post' // Replace with other post type if you want
);
$posts = get_posts( $args );
@jorpdesigns
jorpdesigns / hidden-content-toggle.php
Created July 13, 2021 19:28
Snippet for creating hidden content toggle
<?php
add_shortcode( 'hidden_content', 'hidden_content' );
function hidden_content( $atts, $content = null ) {
$paragraphedText = "<p>" . implode( "</p>\n\n<p>", preg_split( '/\n(?:\s*\n)+/', $content ) ) . "</p>";
return '<div class="hidden-content" style="display: none;">' . $paragraphedText . '</div>';
}
// ADD JS SCRIPT TO FOOTER
add_action( 'wp_footer', 'hidden_content_script' );
@jorpdesigns
jorpdesigns / current-year.php
Created July 13, 2021 19:25
Shortcode that returns current year
<?php
add_shortcode( 'current_year', 'current_year' );
function current_year() {
return date("Y");
}
?>