Skip to content

Instantly share code, notes, and snippets.

View joshuadavidnelson's full-sized avatar

Joshua Nelson joshuadavidnelson

View GitHub Profile
@joshuadavidnelson
joshuadavidnelson / get-post-type-by-taxonomy.php
Last active March 25, 2023 15:02
Get post types that have a specific taxonomy
<?php
/**
* Get post types that have a specific taxonomy (a combination of get_post_types and get_object_taxonomies)
*
* @author Joshua David Nelson, josh@joshuadnelson.com
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2.0
*
* @see register_post_types(), get_post_types(), get_object_taxonomies()
*
* @param string $taxonomy Required. The name of the taxonomy the post type(s) supports.
@joshuadavidnelson
joshuadavidnelson / basic-dropdown-usage.php
Last active February 27, 2023 13:44
Filter wp_dropdown_categories by post type.
<?php
/**
* Using wp_dropdown_categories with the post type filter applied.
*
* @link https://joshuadnelson.com/category-taxonomy-dropdown-filtered-by-post-type/
*/
// Taxonomy dropdown arguments
$args = array(
'taxonomy' => 'department',
@joshuadavidnelson
joshuadavidnelson / taxonomy-archive-list-shortcode.php
Last active December 7, 2022 15:55
Shortcode to create a list or dropdown link to taxonomy archives
<?php
/**
* Shortcode to create a list or dropdown link to taxonomy archives
*
* @author Joshua David Nelson, josh@jdn.im
**/
function jdn_taxonomy_list( $atts ) {
$a = shortcode_atts( array(
'taxonomy' => '',
@joshuadavidnelson
joshuadavidnelson / multilingualpress-cross-post-type.php
Last active August 9, 2022 19:23
Filter the MultilingualPess admin metabox to allow for connecting translations to other post types on remote site.
<?php
/**
* Filter the post search arguments allow for cross-post-type connections.
*
* @param array $args the WP_Query arguments used.
* @return array
*/
function remote_post_search_arguments( $args ) {
$translated_post_types = array( 'post', 'page', 'product' );
@joshuadavidnelson
joshuadavidnelson / dwpb-example-filter-author-archive-post-types.php
Created April 24, 2022 14:43
Disable Blog Example: Add new post types to author archives. By default only posts are shown on author archives, if other post types are to appear on the author archives, pass them with this filter.
<?php
add_filter( 'dwpb_author_archive_post_types', 'dwpb_example_filter_author_archive_post_types', 10, 1 );
/**
* Example: Filter `dwpb_author_archive_post_types` for Disable Blog plugin.
*
* Add new post types to author archives.
*
* By default only posts are shown on author archives, if other post types are to appear
* on the author archives, pass them with this filter.
*
@joshuadavidnelson
joshuadavidnelson / multilingualpress-filter-draft-hreflangs.php
Created March 30, 2022 21:00
Filter the hreflang urls and remove any that are indicative of draft or otherwise non-published content. Assumes that linked translations that are not yet published will show up as /?p=123 instead of pretty permalink.
<?php
/**
* Filter the hreflang urls and remove any that are indicative of draft or otherwise non-published content.
*
* Linked translations that are not yet published will show up as /?p=123 instead of pretty permalink.
*
* @param array $urls the array of language => url hreflang urls.
* @return array
*/
add_filter( 'multilingualpress.hreflang_translations', 'filter_mlp_hreflang_translations' );
@joshuadavidnelson
joshuadavidnelson / remove-post-type-from-search-results.php
Last active March 8, 2022 13:56
Remove a post type from search results, but keep all others
<?php
/**
* Modify query to remove a post type from search results, but keep all others
*
* @author Joshua David Nelson, josh@joshuadnelson.com
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2+
*/
add_action( 'pre_get_posts', 'jdn_modify_query' );
function jdn_modify_query( $query ) {
@joshuadavidnelson
joshuadavidnelson / cpt_has_archive.php
Last active December 11, 2021 10:54
Check if a custom post type supports archives
<?php
/**
* Check if post type supports an archive
*
* @param string $post_type post type name
* @uses get_post_type
* @global object $post
* @returns boolean
* @author Joshua David Nelson
*/
@joshuadavidnelson
joshuadavidnelson / stf-jquery-header-in-archives-only.php
Last active July 2, 2021 16:26
Place jQuery in the head on archive pages. You can use other conditionals in place of `is_archive()` to achieve this behavior for other conditions.
<?php
/**
* Use this filter to force jquery to remain in the head section of the page on archives.
*
* @see https://wordpress.org/support/topic/exclude-jquery-for-other-archive-pages/
* @date 2021-07-02
*/
add_filter( 'stf_jquery_header', 'jdn_archive_header_scripts', 10 );
function jdn_archive_header_scripts( $bool ) {
@joshuadavidnelson
joshuadavidnelson / has-gform.php
Created October 28, 2015 06:45
Check if the page has a gravity form
<?php
/**
* Check if the page has a gravity form.
*
* @since 1.0.0
*
* @param int $id Post or page id, default to false.
*
* @return boolean
*/