I'm comfortable with theme hooks, regularly add features, but struggling with a plugin hook, to filter content. It's at the limit of my knowledge, and I'm trying to understand the process, to no avail.
I'm using GigPress to manage events. It allows you to add a show (date based) and relate that show to a post (standard WP post).
I've created a custom post type — productions — to relate shows to, instead of just the generic blog posts.
The plugin has a hook — gigpress_related_post_types
— to allow you to swap posts for any CPT of your choice.
If I edit the plugin directly, it works, swapping "posts" for "productions". If I add what I hope to be a correct function, it doesn't break anything, but instead ALL posts and posts types are available to choose. Suggesting I'm overriding default behavior, but incorrectly calling my function?
The sample plugin code:
<?php
$related_posts_sql = "SELECT p.ID, p.post_title FROM " . $wpdb->prefix . "posts p WHERE (p.post_status = 'publish' OR p.post_status = 'future')";
/**
* Provides an opportunity to specify other post types as related posts
*
* @param array $related_post_types
*
* @since 2.3.19
*/
$related_post_types = apply_filters('gigpress_related_post_types', ['post']);
if (!empty($related_post_types)) {
$related_posts_sql .= "AND p.post_type IN( '" . implode("','", $related_post_types) . "' )";
}
$related_posts_sql .= " ORDER BY p.post_date DESC LIMIT 500";
$entries = $wpdb->get_results($related_posts_sql, ARRAY_A);
if ($entries != FALSE) {
foreach ($entries as $entry) { ?>
<option
value="<?php echo $entry['ID']; ?>"<?php if (isset($show_related) && $entry['ID'] == $show_related) {
echo(' selected="selected"');
$found_related = TRUE;
} ?>><?php echo gigpress_db_out($entry['post_title']); ?></option>
<?php }
}
Full plugin new show file here:
An example of one of the many functions I've been suggested is:
add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
Ideally, I would also like to offer two CPTs in place of "posts" — both "productions" and "projects".
I'm comfortable with theme hooks, regularly add features, but struggling with a plugin hook, to filter content. It's at the limit of my knowledge, and I'm trying to understand the process, to no avail.
I'm using GigPress to manage events. It allows you to add a show (date based) and relate that show to a post (standard WP post).
I've created a custom post type — productions — to relate shows to, instead of just the generic blog posts.
The plugin has a hook — gigpress_related_post_types
— to allow you to swap posts for any CPT of your choice.
If I edit the plugin directly, it works, swapping "posts" for "productions". If I add what I hope to be a correct function, it doesn't break anything, but instead ALL posts and posts types are available to choose. Suggesting I'm overriding default behavior, but incorrectly calling my function?
The sample plugin code:
<?php
$related_posts_sql = "SELECT p.ID, p.post_title FROM " . $wpdb->prefix . "posts p WHERE (p.post_status = 'publish' OR p.post_status = 'future')";
/**
* Provides an opportunity to specify other post types as related posts
*
* @param array $related_post_types
*
* @since 2.3.19
*/
$related_post_types = apply_filters('gigpress_related_post_types', ['post']);
if (!empty($related_post_types)) {
$related_posts_sql .= "AND p.post_type IN( '" . implode("','", $related_post_types) . "' )";
}
$related_posts_sql .= " ORDER BY p.post_date DESC LIMIT 500";
$entries = $wpdb->get_results($related_posts_sql, ARRAY_A);
if ($entries != FALSE) {
foreach ($entries as $entry) { ?>
<option
value="<?php echo $entry['ID']; ?>"<?php if (isset($show_related) && $entry['ID'] == $show_related) {
echo(' selected="selected"');
$found_related = TRUE;
} ?>><?php echo gigpress_db_out($entry['post_title']); ?></option>
<?php }
}
Full plugin new show file here: https://codeshare.io/50MKJg
An example of one of the many functions I've been suggested is:
add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
Ideally, I would also like to offer two CPTs in place of "posts" — both "productions" and "projects".
Share Improve this question edited Apr 17, 2019 at 11:27 staffsdesigner asked Apr 17, 2019 at 9:46 staffsdesignerstaffsdesigner 34 bronze badges 4 |1 Answer
Reset to default 0You're on the right path with your add_filter
, but this is the right way to implement filters.
add_filter( 'gigpress_related_post_types', 'my_related_post_types' );
function my_related_post_types( $post_types ) {
return array( 'productions' );
}
The hook add_filter
must call a function that returns something to pass to the apply_filters
in the code you referenced.
You should add in whatever logic you need to return the correct post types and handle the default as well. For example:
add_filter( 'gigpress_related_post_types', 'my_related_post_types' );
function my_related_post_types( $post_types ) {
// Assuming you set an option to control which post types should be used.
$custom_gig_post_types = get_option( 'gig_post_types', false );
if ( $custom_gig_post_types ) {
return $custom_gig_post_types;
}
return $post_types;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745579540a4634172.html
$wpdb
and$show_related
. Consider getting a proper IDE like PHPStorm. It has WordPress support and coding standards built-in. – norman.lol Commented Apr 17, 2019 at 10:01add_filter( 'gigpress_related_post_types', array( 'productions' ) ); if ( ! empty( $related_post_types ) ) { $related_posts_sql .= "AND p.post_type IN( '" . implode( "','", $related_post_types ) . "' )"; }
– staffsdesigner Commented Apr 17, 2019 at 11:07