How to display all pdf files with custom fields - wordpress

I have found a code to display all media files, but how do I list all pdf's only with custom fields
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>

Read this answer in detail and try this.
$extension = 'pdf';
$uploads = wp_upload_dir();
$files = new \FilesystemIterator(
$uploads['path'],
\FilesystemIterator::SKIP_DOTS
| \FilesystemIterator::FOLLOW_SYMLINKS
);
$html = [];
foreach ( $files as $pdf )
{
/** #noinspection PhpIncludeInspection */
if (
! $files->isDir()
&& $extension === $files->getExtension()
)
$html[] = $files->getRealPath();
}
You can then easily craft your final MarkUp using for e.g. native PHPs explode() function:
printf(
"<ul><li>%s</li></ul>",
explode( "</li><li>", $html )
);
Helping Link:
https://wordpress.stackexchange.com/questions/214515/list-and-show-uploaded-pdf-files-dynamically#answer-215516

Related

how to customize shop page query by woocommerce hook?

I need a help regarding archive-product hook.
i am writing this code in functions.php file then the products will shown before a header(screenshot is given),
how i can display products in properly??
please help me!
function so_27975262_product_query( $q, $details = [] ){
$details = warehouse_detail();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
do_action('woocommerce_shop_loop');
$prod_img = wp_get_attachment_url(get_post_thumbnail_id(get_the_id()));
$product = wc_get_product(get_the_id());
$price = $product->get_price();
$werehouseDetails = unserialize(get_post_meta(get_the_ID(), 'warehouse_id', true));
$billing_state = get_user_meta(get_current_user_id(), 'billing_state', true);
$warehouse_qty = [];
foreach ($werehouseDetails as $werehouseDetail) {
if (in_array($werehouseDetail, $details)) {
$warehouse_qty = get_post_meta(get_the_ID(), 'warehouse_qty_' . $werehouseDetail, true);
if ($warehouse_qty > 0) { // Shifa End 20-10-22
$products = wc_get_template_part('content', 'product');
}
}
}
endwhile; wp_reset_query();
endif;
$q->set('query',$products);
}
add_action( 'woocommerce_product_query', 'so_27975262_product_query' );

How to get wordpress empty content list

i make two list theme
1.the posts have featured image and title, but empty content. i want get this list.
2.and other posts with content list.
I don't have any idea, help me please
Try to use below code:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
$content = get_the_content();
if($content == '') {
array_push( $blank_posts, $post);
}
endwhile;
endif;
/* print all blank content posts */
//echo "<pre>"; print_r($blank_posts);
/* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "ID= ". $pst->ID . ', '. "Title= ". $pst->post_title .'<hr />';
}
}

wordpress custom Query, Two columns of content, How do I get two paging function?

There are many pages, but how to get paging?
Two columns of content have two paging.
i need paging for Two columns of content.
how to do this?
and if you can tell me how to add the function of the comments, I will thank you even more.
look this:
https://stackoverflow.com/a/45451489/1266305
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$content_post =array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
global $post;
$content = get_the_content();
if(empty($content)) {
array_push( $blank_posts, $post);
}else{
array_push( $content_post, $post);
}
endwhile;
endif;
/* blank content posts */ /* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "<pre>"; print_r($blank_posts);
echo $pst->post_title;
}
};
/* have content posts */
if(!empty($content_post)){
foreach ($content_post as $pst) {
echo '<a href='. $pst->guid .'>' .$pst->post_title. '</a> ';
}
}

ordering wordpress post_meta - lowest to highest

I am currently looping through all posts and displaying a post_meta value like this:
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
foreach ( $getLowestPrice as $post ){
get_post_meta( $post->post_id, '_wholesale_price', false );
}
Is there a way to order the results, lowest -> highest? At the moment they are getting displayed randomly, or as they were entered.
use the following code
<?php
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
$all_post = array();
foreach ( $getLowestPrice as $post ){
$all_post[] = $post->post_id;
}
$query = new WP_Query( array( 'post__in' => $all_post, 'orderby' => 'meta_value', 'meta_key' => '_wholesale_price','order' => 'ASC') );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div>' . get_post_meta( $get_the_ID(), '_wholesale_price', false );() . '</div>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>`
This looks a bit wrong to me. If you're trying to order posts by the value of a meta key (unless I'm mistaken, that is what you're doing) then I would normally go about it using WP_Query()
global $wp_query;
$args = array(
'post_type' => '[YOUR POST TYPE]',
'meta_key' => '_wholesale_price',
'orderby' => 'meta_value meta_value_num', // meta_value_num for value
'order' => 'ASC' // or DSC for low/high
);
$wp_query - new WP_Query( $args );
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// Your loop
}
}

wp how to exclude filter from the custom query

I am trying to exclude a filter (slug) from a loop. I am researched exclude and have tried it in various places in the code. Usually I break the page. This is the code, I am attempting to change to exclude the slug 20. It is a filter named 'american'. I have tried to exclude at the beginning of the array, didn't work; then, I tried after the foreach($catz as $cat) section. I tried this ‘exclude=20&title_li=' . I tried cat=-20 and various other combinations. Any help would be very very appreciated.
// The Custom Query
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => $counter_folio,
'paged' => $paged,
'order' => 'DESC'
);
query_posts( $args );
while( have_posts() ) : the_post();
$color = substr(get_option('dcb_dynamic_color'), 1);
// Get the original thumbnail of the post
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
$excerpt = get_the_excerpt();
// Get the custom fields
$vimeo = get_post_meta($post->ID, "vimeo", true);
$youtube = get_post_meta($post->ID, "youtube", true);
// Get the filter > Category of item
$catz = wp_get_object_terms($post->ID,'filters');
foreach($catz as $cat){
$currcat = $cat->slug;
$catname = $cat->name;
break;
}
$counter++;
?>
If you want to filter the post with id 20 simple exclude it with the following code
// The Custom Query
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => $counter_folio,
'paged' => $paged,
'order' => 'DESC'
);
query_posts( $args );
while( have_posts() ) : the_post();
$color = substr(get_option('dcb_dynamic_color'), 1);
// Get the original thumbnail of the post
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
$excerpt = get_the_excerpt();
// Get the custom fields
$vimeo = get_post_meta($post->ID, "vimeo", true);
$youtube = get_post_meta($post->ID, "youtube", true);
// Get the filter > Category of item
$catz = wp_get_object_terms($post->ID,'filters');
foreach($catz as $cat){
if($cat->slug != 'american')
{
$currcat = $cat->slug;
$catname = $cat->name;
break;
}
}
$counter++;

Resources