Skip to content

Instantly share code, notes, and snippets.

@rabidgadfly
rabidgadfly / wp_getCPTbyCategory.php
Last active November 20, 2015 22:18
Wordpress: Query custom post type by category
<?php
// NOTE: It is recommended to use pre get posts instead of query posts
query_posts( array( 'post_type' => 'listing', 'listing_category' => 'the-category' ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; endif; wp_reset_query(); ?>
@rabidgadfly
rabidgadfly / wp_CPT-listing-page-template.php
Last active November 30, 2015 20:00
This page template will provide a listing of all posts under the specified post type, in this case 'project'
<?php
/**
* Template Name: Project Listing
*
* Selectable from a dropdown menu on the edit page screen.
*/
?>
<?php get_header(); ?>
@rabidgadfly
rabidgadfly / wp_filter_posts_by_custom_field
Last active December 2, 2015 03:52
WordPress page template to display posts of a specific post type filtered by the id of an Advanced Custom Field 'Field Object'
<?php
/**
* WordPress Page Template: Project Listing
*/
?>
<?php get_header(); ?>
<div id="container">
<div id="content">
<?php
//
// Source: http://snippets.khromov.se/dynamically-populating-advanced-custom-fields-values/
//
//Populate the Select field field_5587fccf24f38 with dynamic values
add_filter('acf/load_field/key=field_5587fccf24f38', function($field) {
//These can be dynamically generated using any PHP code
$field['choices']['one'] = 'Choice one';
$field['choices']['two'] = 'Choice two';
@rabidgadfly
rabidgadfly / wp_acf_dynamicallyPopulateSelect_1of2.php
Last active February 20, 2023 12:09
Examples of how to dynamically populate an ACF select list
<?php
/*
* In this example, we will load a textarea field from the options page (textarea field is called my_select_values)
* and use the text on each line as a option for the select field’s choices. The select field in this example has a name of “color”.
* Note: This is example 1 of 2
*
* Source: http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
*
* Add to your functions.php
*/
@rabidgadfly
rabidgadfly / wp_acf_update_field_examples.php
Created February 12, 2016 01:47
Methods of updating data in an advanced custom field
<?php
/*
* update a text field on the current post (using the field_name)
*/
$field_name = "text_field";
$value = "some new string";
update_field( $field_name, $value );
@rabidgadfly
rabidgadfly / bp_custom.php
Last active February 25, 2016 15:08
Miscellaneous BuddyPress code snippets
<?php
/*
* Example of how to change the permalink of groups.
* The following code changes http://yoursite.com/groups
* to http://yoursite.com/organizations.
* This is done in bp_custom.php (Google for more info)
*/
define( 'BP_GROUPS_SLUG', 'organizations' );
?>
@rabidgadfly
rabidgadfly / do_shortcode_buffer.php
Last active February 26, 2016 00:29
How to use PHP buffer to place an ACF form inside a Shortcodes Ultimate Accordion Spoiler
<?php
/*
* Ran into a need for this when I wanted to place
* an ACF form into a Shortcodes Ultimate spoiler.
* Problem was that the form would render outside of the
* spoiler accordion. Solution was to use PHP Output Buffering.
*/
$args = array(
'post_id' => 'new_post',
'post_title' => true,
@rabidgadfly
rabidgadfly / wp_acf_set_field_value.php
Last active February 26, 2016 00:28
How to set the value of an ACF field in a form
<?php
/*
* Example of setting the value of an acf custom field on form submission.
* In this example I'm saving the BuddyPress group id into field_56bd24ccc6fd3.
*/
//Populate the 'group' custom field (field_56bd24ccc6fd3) with dynamic values
add_filter('acf/load_field/key=field_56bd24ccc6fd3',
function($field) {
global $bp;
@rabidgadfly
rabidgadfly / wp_acf_add_cat_on_save.php
Created February 26, 2016 00:27
How to add a category and attach it to the post on ACF save_post
<?php
/*
* Example of hooking into the Advanced Custom Field save process
* to add a category to the taxonomy and attach it to the post.
* This particular example retrieves the value from a custom field
* named 'group_slug' and uses it as a category for the post.
*/
add_filter('acf/save_post',
// Using a closure here but could be a separate function
function($post_id) {