Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Last active December 29, 2017 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BhargavBhandari90/35ea91a84632d47022d1210cb132692f to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/35ea91a84632d47022d1210cb132692f to your computer and use it in GitHub Desktop.
Read comment below for how to use.
<?php
/**
* Get media of perticular topic or forum
*
* @param int $item_id Id of forum
* @return array Array of media ids associated with the forum
*/
function rtcamp_get_perticular_forum_media( $item_id ) {
if ( empty( $item_id ) ) {
return;
}
$medias = array();
// Get the topics of forum.
$topics = bbpress_get_data( $item_id, 'topic' );
if ( ! empty( $topics ) && is_array( $topics ) ) {
foreach( $topics as $topic_id ) {
// Get the replys of topic
$replys = bbpress_get_data( $topic_id, 'reply' );
if ( ! empty( $topics ) && is_array( $topics ) ) {
foreach( $replys as $reply ) {
// Get medias attached to topic.
$medias[] = rtmedia_get_medias( $reply );
}
}
}
}
if ( ! empty( $medias ) ) {
$medias = array_values( array_filter( $medias ) );
}
return $medias;
}
/**
* Delete media of perticular topic or forum
*
* @param int $item_id Id of forum
* @return array Array of media ids associated with the forum
*/
function delete_media_of_forum( $item_id ) {
if ( empty( $item_id ) ) {
return;
}
// Get medis from forum id.
$medias = rtcamp_get_perticular_forum_media( $item_id );
if ( ! empty( $medias ) ) {
global $wpdb;
foreach ( $medias as $media_id ) {
$where = array(
'media_id' => $media_id
);
// Delete attachment.
wp_delete_attachment( $media_id );
// Delete entry from rtmedia table.
return $wpdb->delete( $wpdb->prefix . 'rt_rtm_media' , $where );
}
}
}
// Get the data.
function bbpress_get_data( $item_id, $context ) {
$args = array(
'no_found_rows' => true,
'fields' => 'ids',
'posts_per_page' => 500,
'post_parent' => $item_id,
'post_status' => 'publish',
'post_type' => $context
);
$data = new WP_Query( $args );
return $data->posts;
}
// Get medias by reply.
function rtmedia_get_medias( $id ) {
$model = new RTMediaModel();
$media = $model->get_media( array(
'context_id' => $id,
), false, false );
if ( empty( $media ) ) {
return;
}
return $media[0]->media_id;
}
@BhargavBhandari90
Copy link
Author

If you want to fetch the media associated with particular forum the use following function and pass forum ID as an argument

rtcamp_get_perticular_forum_media( $item_id )

If you want to fetch and delete those media, then use the following function and pass forum ID as an argument

delete_media_of_forum( $item_id )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment