Skip to content

Instantly share code, notes, and snippets.

@gaupoit
Last active October 15, 2019 04:42
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 gaupoit/fae92e6e0a140b833a63b68881dcecf8 to your computer and use it in GitHub Desktop.
Save gaupoit/fae92e6e0a140b833a63b68881dcecf8 to your computer and use it in GitHub Desktop.
Filter the URLs protected by Prevent Direct Access plugin
<?php
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => '_pda_protection',
'value' => '1'
)
)
);
$query = new WP_Query( $args );
// Here, you can get the array of posts by $query->posts
$attachments = get_attachment_urls( $query->posts );
// Or display the result in theme
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Post data goes here.
?>
<h2><?php the_title(); ?></h2>
<p>
<?php the_content(); ?>
</p>
<?php
}
}
/**
* Get user roles in settings
* @param array Query's result from WP_Query
*
* return array
*/
function get_attachment_urls( $query_result ) {
return array_map( function ( $attachment ) {
$attachment->url = wp_get_attachment_url( $attachment->ID );
return $attachment;
}, $query_result );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment