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;
}
Related
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,
);
Working with a plugin that provides a shortcode with only a couple of parameters. https://wordpress.org/plugins/radio-station I'm using the [list-shows] shortcode. I would like to also pull in an excerpt of the content, I imagine that would be applicable to a lot of situations and improve my understanding of shortcodes.
I was able to add in the thumbnail to the output, but not the content, which seems to be referred to in the atts of the other shortcodes as 'show_desc'. You can see the output on this page: http://www.kzmuradio.org/kzmu-programs/
Here is the code for the shortcode. Apologies for my ignorance. Learning curve.
/* Shortcode for displaying a list of all shows * Since 2.0.0*/
function station_shortcode_list_shows($atts) {
extract( shortcode_atts( array(
'genre' => '',
'show_desc' => 1
), $atts ) );
//grab the published shows
$args = array(
'numberposts' => -1,
'offset' => 0,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'show',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'show_active',
'value' => 'on',
)
)
);
if($genre != '') {
$args['genres'] = $genre;
}
$shows = get_posts($args);
//if there are no shows saved, return nothing
if(!$shows) {
return false;
}
$output = '';
$output .= '<div id="station-show-list">';
$output .= '<ul>';
foreach($shows as $show) {
$output .= '<li>' . get_the_post_thumbnail($show->ID, 'thumbnail') . ''.get_the_title($show->ID).'</li>';
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
return $output;
You can access all the data through $show in your foreach-loop. To see what they contain, you can use print_r():
print_r( $show );
For excerpt and content you can use this:
echo $show->post_excerpt;
echo wpautop( $show->post_content );
See also: https://codex.wordpress.org/Function_Reference/$post
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();
}
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;
as you can read, I'm having trouble sorting the gallery in the order I want it to. I'm trying to have it sorted just like in the Drag&Drop Interface, where you edit your gallery. That's the same order as in the id attribute in the shortcode. I just can't figure out what value to assign to $orderby. i tried 'ID', 'menu_order' and 'post__in', but no changes.
Do you have any advice.
add_filter('post_gallery', 'fgf_gallery', 10, 2);
function fgf_gallery($output, $attr) {
global $post;
static $instance = 0;
$instance++;
$id = $post->ID;
$order = 'ASC';
$orderby = 'ID';
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
if ( empty($attachments) ) return;
$output = "<ul id='gallery-{$instance}' class='gallery'>";
foreach ( $attachments as $id => $attachment ) {
$output .= "<li class='gallery-item'>";
$output .= "<img src='".$thumb_src."'>";
$output .= "<h1>".$attachment->post_title."</h1>";
if ( trim($attachment->post_excerpt) ) {
$output .= "
<p class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
<p>";
}
$output .= "</li>";
}
$output .= "</ul>\n";
return $output;
}
Thanks. I appreciated any hint to further documentation as well.
I found what I needed in the wp-includes/media.php file
you get the order from your shortcode with "post__in".
function fgf_gallery_2($output, $attr) {
static $instance = 0;
$instance++;
$order = 'ASC';
$orderby = 'post__in';
$include = $attr['ids'];
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
if ( empty($attachments) ) return;
$output = "<ul id='gallery-{$instance}' class='gallery'>";
foreach ( $attachments as $id => $attachment ) {
/* do what you want here */
}
$output .= "</ul>\n";
return $output;
}
works for me.