Description
Implement [bw_related] shortcodeTo take out the hard work listing items which are related to the current item through "noderef" type fields Here we have the original shortcode from post ID 401 which is an instance of an "oik_shortcode"
[bw_list post_type=shortcode_example meta_key=_sc_param_code meta_value=401]The lookup performed in bw_list is for posts of type "shortcode_example" with meta fields with a name of "_sc_param_code" that have a value of 401. In other words it lists all the "shortcode examples" which refer to this particular post. _sc_param_code is the name given to a noderef type field which references the "shortcode_example" post type. We 'hijacked' this field when defining the fields for the "shortcode_example" custom post type. It had previously been used for linking shortcode parameters to the shortcode. Rather than having to get the meta_value right in each usage of the shortcode we would like it to default to the current post id. We're going to change bw_get_posts() to achieve this for ALL post related shortcodes. BUT first we want to create the [bw_related] shortcode just to see how easy it is. ie. We'll code [bw_related post_type=shortcode_example meta_key=_sc_param_code] omitting the meta_value parameter. This shortcode can therefore also be used to lookup the $bw_fields / $bw_mapping table to find references to posts of the current post_type and therefore find the meta_key names to use thus enabling the shortcode to be coded as [bw_related] Now wouldn't that be nice? Note: the post_type defaults to the current post type… but that's not what we want except when we want to find links to other posts of the same type. We still need to know the value for the meta_key. We do need to know the current post type in order to find which fields refer to it.
Usage
bw_related( $atts, $content, $tag );Parameters
- $atts
- ( mixed ) optional –
- $content
- ( mixed ) optional –
- $tag
- ( mixed ) optional –
Returns
voidSource
File name: oik-fields/shortcodes/oik-related.phpLines:
1 to 41 of 41
function bw_related( $atts=null, $content=null, $tag=null ) { //bw_trace2(); //bw_backtrace(); oik_require( "shortcodes/oik-list.php" ); oik_require( "includes/bw_posts.php" ); $post_type = bw_array_get( $atts, "post_type", null ); $meta_key = bw_array_get( $atts, "meta_key", null ); $atts['post_parent'] = bw_array_get( $atts, 'post_parent', 'no' ); $tag = bw_array_get( $atts, "tag", null ); $category_name = bw_array_get( $atts, "category_name", null ); $by = bw_array_get( $atts, "by", null ); if ( $tag ) { $atts['tag'] = bw_query_taxonomy_value( $tag ); } elseif ( $category_name ) { $atts['category_name'] = bw_query_taxonomy_value( $category_name ); } elseif ( $by ) { $ids = bw_query_field_values( $by ); $atts['post__in'] = bw_array_get_unkeyed( $ids ); unset( $atts['by'] ); } else { if ( $post_type && $meta_key ) { // they've specified the post type and meta_key so we don't have to look for it } else { $meta_key = bw_query_related_fields( $atts ); } $atts['meta_value'] = bw_related_meta_value( $atts, $meta_key ); } $format = bw_array_get( $atts, "format", null ); if ( $format === 'T') { oik_require( "shortcodes/oik-table.php"); $result = bw_table( $atts ); } elseif ( $format ) { oik_require( "shortcodes/oik-pages.php" ); $result = bw_pages( $atts ); } else { $result = bw_list( $atts ); } return( $result ); }View on GitHub
Called by
Invoked by
Calls
1 to 5 of 5
- bw_array_get() – Return the array[index] or array->index (for an object) or a default value if not set
- bw_query_field_values() – Return the field values for the given post
- bw_query_related_fields() – Determine what we should be listing based on the current post
- bw_query_taxonomy_value() – Determine the tag or category value to use if the given value is not a tag or category slug
- bw_related_meta_value() – Determine a meta_value based on the field type, the specified meta_value and possibly the "meta_compare" parameter
