Wp query in meta_box breaks wp editors - wordpress

I just added a meta_box to my custom post_type in wordpress and my two extra wp_editors just dissapears.
I have code like this:
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_page' => -1,
'post_mime_type' =>'application/pdf, image/jpeg, image/gif, image/jpg, image/png'
);
$tmp = $post;
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//some code for showing result etc...
}
}
$post = $tmp;
wp_reset_postdata();
I have narrowed it down to this 'post_status' => 'any'
If I change post status to something else, like:
post, page, custom etc...
not any or inherit..
I get my wp editors back but dont get any result from the query...
I'm a missing something here?

Found a fix! Changed WP_Query to query_posts() if someone else faces the same problem...
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_mime_type' =>'application/pdf, image/jpeg, image/gif, image/jpg, image/png'
);
$tmp = $post;
// The Query
$the_query2 = query_posts( $args );
if ( $the_query2 ) {
echo '<select class="bw_selector" name="bw_vendor_material">';
echo '<option value="">Choose attachment:</option>';
// The Loop
foreach ($the_query2 as $key => $value) {
//print_r($value->ID);
echo '<option value="'.$value->ID.'">' . $value->post_mime_type .': '. get_the_title($value->ID) . '</option>';
}
echo '</select>';
echo '<br/>';
}
else {
echo "No posts found";
}
// Restore original Post Data
wp_reset_query();
$post = $tmp;

Related

wp_query in shortcode not working as expected

I created a simple shortcode
add_shortcode('lichthidau', 'hp_lich_thi_dau');
function hp_lich_thi_dau( $atts ) {
$output = '';
extract( shortcode_atts( array( 'posttype' => 'lich' ), $atts) );
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$itemprop = '';
if ( 'microdata' === generate_get_schema_type() ) {
$itemprop = ' itemprop="text"';
}
echo '<div class="bang-lich">';
//while ( $the_query->have_posts() ) {
$output = $the_query->found_posts;
//}
echo '</div>';
}
wp_reset_postdata();
return $output;
}
Then put it in Gutenberg shortcode block [lichthidau] in a page (ID = 106, for example).
Without while loop, it's showing 2, which is the count of returning posts, and it's correct. However, if I enable while loop, it's taking the current page ID (106), and creating unlimited loops, while the expected result should be only two number 2.
Can anyone advice why and how to fix, please?
Thanks.
The first problem is that you're using echo in the shortcode output. The shortcode can only return content, and echo will produce unexpected results.
The second problem is trying to output the $output = $the_query->found_posts; within your loop. If you return something else, it will work.
This returns your loop with the post titles.
add_shortcode( 'lichthidau', 'hp_lich_thi_dau' );
function hp_lich_thi_dau( $atts ) {
$output = '';
extract( shortcode_atts( array( 'posttype' => 'lich' ), $atts ) );
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$itemprop = '';
if ( 'microdata' === generate_get_schema_type() ) {
$itemprop = ' itemprop="text"';
}
$output = '<div class="bang-lich">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$output .= get_the_title( get_the_ID() ) . '</br>';
}
$output .= '</div>';
}
wp_reset_postdata();
return $output;
}

WordPress search not working properly

I have a recipe website and search option in it. Now i have recipe name CAKE and when i search CAKES then its not showing result. Its fine with CAKE or CAK . I want result with LIKE query. Please help me out.
Here is my custom search args code:
<?php $args = array( 'posts_per_page' => -1, 'post_type' => 'recipes', 's' => $_GET['s'] );
$myposts = get_posts( $args );
if ( empty( $myposts ) ) {
echo 'No results found in Recipe!';
} else { ?>
//show recipes
<?php wp_reset_postdata();
} ?>
Try this
$args = array('posts_per_page' => -1, 'post_type' => 'recipes','s' => $_GET['s']);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//whatever you want to do with each post
}
} else {
// no posts found
}
After so many research i found an alternate. I change plural terms into singular with some php manipulation. Here is i am sharing it.
$str = $_GET['s'];
$result = rtrim($str, 's');
$result_sing = str_pad($result, strlen($str) - 1, 's');
$args = array(
'posts_per_page' => -1,
'post_type' => 'recipes',
's' => $result_sing,
);

Get pages with specific custom field value not working

I have created a custom field in some pages and I need to loop through these pages and print their info. The code I'm using isn't working (the foreach is not looping).
Here's the code:
<?php
$args = array(
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$pages = get_pages($args);
foreach ($pages as $page) {
echo "<p>$page->post_title</p>";
}
wp_reset_postdata();
?>
And here's the page custom field config (wordpress in portuguese):
What's wrong with it?
Solved with this code:
<?php
$args = array(
'post_type' => 'page',
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$myPages = new WP_Query($args);
while ($myPages->have_posts()) : $myPages->the_post();
echo "$post->post_title";
endwhile;
wp_reset_postdata();
?>
<?php
$args = array(
'meta_key' => 'categoria-pagina',
'meta_value' => 'programas'
);
$custom_query = new WP_Query( $args );
// The Loop
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
echo '<p>' . get_the_title() . '</p>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
That should do the trick for you.

Get all posts beginning with letter A

How can i get all poasts beginning with the letter A (in the post_title)?
My idea was to use regex but this code dont work.
$my_custom_query_args = array(
'cat' => '1',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 25,
'offset' => 0,
'value' => '^'.$letter.'',
'compare' => 'REGEXP'
);
Using post_where , this action code be use in custom template.
add_action( 'posts_where', 'startswithaction' );
function startswithaction( $sql ){
global $wpdb;
$startswith = get_query_var( 'A' );
if( $startswith ){
$sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $startswith.'%' );
}
return $sql;
}
OR you can get all records starts with letter A by SQL query and pass the post ids in WP_QUERY.
//get all post IDs for posts start with letter A, in title order,
//display posts
global $wpdb;
$first_char = 'A';
$postids = $wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $first_char;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
}

What is the function got get all the media files wordpress?

Can anyone suggest me what is the function to get all the images stored for wordpress? I just need to list all the images seen under menu Media of the wordpress admin.
Thanks in advance
Uploaded images are stored as posts with the type "attachment"; use get_posts() with the right parameters. In the Codex entry for get_posts(), this example:
<?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();
}
}
?>
...loops through all the attachments and displays them.
If you just want to get images, as TheDeadMedic commented, you can filter with 'post_mime_type' => 'image' in the arguments.
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>

Resources