0

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".

  • 1
    Undefined variables in your code: $wpdb and $show_related. Consider getting a proper IDE like PHPStorm. It has WordPress support and coding standards built-in. – leymannx Apr 17 '19 at 10:01
  • Thanks @leymannx — that's the plugin itself, and only a sample, relating to teh post types... The plugin author suggested only the hook "gigpress_related_post_types" was needed to target it in functions, but can't offer support or details. – staffsdesigner Apr 17 '19 at 10:38
  • What’s your code that you’re using to try and change the value. – Jacob Peattie Apr 17 '19 at 10:56
  • Thanks @JacobPeattie... I've tried a million variations... [well, ten+?) Things along the lines of, have been suggested to me: 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 ) . "' )"; } – staffsdesigner Apr 17 '19 at 11:07
0

You'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;
}
| improve this answer | |
  • You're a legend, thank you so much! Not only does it work, but I understand why, I think! Can I buy you a beer?! – staffsdesigner Apr 17 '19 at 15:20
  • Haha. I appreciate the offer. Happy to help. – MikeNGarrett Apr 17 '19 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.