Successfully reported this slideshow.
Your SlideShare is downloading. ×

Advanced Custom Fields - Beyond the Basics

Upcoming SlideShare
Theme customization
Theme customization
Loading in …3

Check these out next

1 of 11 Ad

More Related Content

Viewers also liked (20)

Similar to Advanced Custom Fields - Beyond the Basics (20)


Recently uploaded (20)

Advanced Custom Fields - Beyond the Basics

  1. 1. Advanced Custom Fields Beyond the Basics
  2. 2. Merrill M. Mayer Kool Kat Web Designs Seattle, WA USA --------------------------------- --------------------------------- @koolkatweb
  3. 3. Overview  Custom Queries  Custom Previous and Next Post Functionality  Custom Dashboard Presentation  Speaker Program:
  4. 4. Speaker Date Custom Field
  5. 5. Speaker Program Grid View
  6. 6. Query Parameters post_type => rotary_speakers posts_per_page => -1 orderby => meta_value meta_key => speaker_date More information on the grid can be found at 2014-speaker-program-custom-grid-and-advanced- search/.
  7. 7. Speaker Program Single View
  8. 8. Filters for Next and Previous Links add_filter('get_previous_post_join', 'rotary_post_join'); add_filter('get_next_post_join', 'rotary_post_join'); add_filter('get_previous_post_where', 'rotary_prev_post_where'); add_filter('get_next_post_where', 'rotary_next_post_where'); add_filter('get_previous_post_sort', 'rotary_prev_post_sort'); add_filter('get_next_post_sort', 'rotary_next_post_sort'); add_filter('next_post_link', 'rotary_filter_next_post_link'); add_filter('previous_post_link', 'rotary_filter_previous_post_link'); Full implementation at: prev-and-next-post-filters/
  9. 9. Speaker Dashboard Summary
  10. 10. Dashboard Filters and Actions add_filter('manage_rotary_speakers_posts_columns' , 'rotary_speakers_cpt_columns'); add_action( 'manage_rotary_speakers_posts_custom_column' , 'rotary_custom_speaker_column_data', 10, 2 ); add_filter('manage_edit-rotary_speakers_sortable_columns', 'rotary_column_register_sortable'); add_filter( 'request', 'rotary_speaker_column_orderby' ); Full implementation at: speaker-program-dashboard-filters/
  11. 11. Thank You

Editor's Notes

  • Welcome to Advanced Custom Fields – beyond the basics. Today we are going to cover a brief introduction of using Advanced Custom fields as part of a custom post type to implement a speaker program for the Bellevue Breakfast Rotary Club.
  • First, a little about me. I am a freelance web developer, mostly working with WordPress. I enjoy working directly with designers to develop custom themes for clients.
  • We will focus this presentation on the use of the speaker date field.
  • In setting up a Speaker Program implementation, one item that gained a lot of importance was the speaker date – the date on which a particular speaker actually presented at the Bellevue Breakfast Rotary Club. The idea of the post date and handling scheduled posts was too complicated for the club which is why we chose to use a custom field. The Speaker date is defined as a datepicker field via the Advanced Custom Fields plugin. Once the we decided on using a custom date field, all kinds of issues arose. For example, how do we get the speaker posts and in what order, how do we show the next and previous speaker in the single post and what should be displayed in the dashboard.
  • This is the list of speakers (current and previous). Notice that speakers are listed in descending speaker date order – not post date order.
  • To achieve the correct order and to get the correct custom posts, we pass these arguments to WP_Query. The grid itself uses the jQuery Datatables plugin to display the results. The permalink is stored in a hidden column which allows us to navigate to the single Rotary Speakers post when a grid row is clicked.
  • Clicking on a grid row brings up the single post for the speaker. Notice that the next and previous links have the next and previous speaker date.
  • To get the next and previous to work using the speaker date, we must implement several filters for both the previous and next post links. These filters change the actual query used to process the previous and next links. So, what do these all do?

    Well, the custom fields are stored in the post meta table. This is different from the post date, which is in the posts table. So the JOIN filter is used to join the post table and post meta table together. (actually INNER JOIN matching columns in both the Posts and Posts Meta table) The join uses the post ID.

    The WHERE filter sets the criteria for the query as does any other WHERE clause. We actually use two of the same fields as the WP_Query shown earlier, and include the meta_key and meta value.

    The SORT filter overrides the order by clause allowing us to sort by the meta_value column in ASCENDING sequence for the “NEXT” llink and DESCENDING sequence for the “PREVIOUS” link. Remember that in this case, our meta_value is the speaker_date that we defined with Advanced Custom Fields.
  • The dashboard shows our custom fields. The “Cols” field is in from a plugin but the remaining fields are part of the Rotary speakers custom post type and are all defined using advanced custom fields. The default order is by speaker date, descending but may be sorted by first and last name, too.
  • First, notice that all of the “manage” filters and actions include the specific custom post name, in this case, “rotary_speakers”.

    manage_${post_type}_posts_columns allows the addition and removal of dashboard columns for the specific post type. As an example, we remove the post date and author.

    manage_${post_type}_posts_column retrieves the data for a specific column. For example, the data for the speaker’s first or last name

    manage_edit-${post_type}_sortable_columns sets which columns are sortable. Here, the speaker date, speaker’s first name and speaker’s last name can be sorted.

    Filtering the request is tricky as you don’t want to impact other areas of the dashboard. Be sure to check for your custom post type and/or order by variable. I have seen reference to using pre_get_posts for this instead of the request filter but have not yet tried that out.