WordPress search not working properly - wordpress

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,
);

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: (un-)related post that is not in current post tag

I show related posts with the same tag at the bottom of a post, but I also want to add one random post, that has not the tag, like "try this completely different thing".
I've tried with tag__not_in, but my code doesn't work:
$tag_id = get_queried_object()->term_id;
$args = [
'post__not_in' => array( get_queried_object_id() ),
'tag__not_in' => array( $tag_id ),
'posts_per_page' => 1,
'orderby' => 'rand'
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
Sorry, I don't really know how to code, I just try to understand Wordpress' code and how I can modify it.
How do I get the posts tag id into the array?
Update> This works:
global $post;
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
// just the test echo $tag->slug;
$tag = get_term_by('name', $tag->slug, 'post_tag');
$args = array(
'posts_per_page' => 1,
'tag__not_in' => array($tag->term_id),
'orderby' => 'rand',
);
}
}
$query = new WP_Query($args);
What's the error you're getting?
Regardless, I don't think you need 'post__not_in' because the 'tag__not_in' will take care of that for you.
After that, I'd look at the $tag_id = get_queried_object()->term_id; and make sure you're assigning the value you want to that variable. Echo $tag_id somewhere and confirm it's what you want.
Finally, I think you need your new wp_query( $args ); to be new WP_Query( $args );

How to Hide post that have same category

is there someone who knows how to hide post that have same category?
I have Post 1, Post 2, Post 3 that have same category. Can i just show the latest post ( post 1) at my homepage, and hide other post ( post 2 and post 3 )
One way of doing that is to have three separate wp_queries.
//Query 1
$args1 = array(
'cat' => '1',
'nopaging' => true,
'posts_per_page' => '1',
); $query_1 = new WP_Query( $args1 );
if ( $query_1->have_posts() ) {
while ( $query_1->have_posts() ) {
$query_1->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
//Query 2
$args2 = array(
'cat' => '2',
'nopaging' => true,
'posts_per_page' => '1',
); $query_2 = new WP_Query( $args2 );
if ( $query_2->have_posts() ) {
while ( $query_2->have_posts() ) {
$query_2->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
//Query 3
$args3 = array(
'cat' => '3',
'nopaging' => true,
'posts_per_page' => '1',
); $query_3 = new WP_Query( $args3 );
if ( $query_3->have_posts() ) {
while ( $query_3->have_posts() ) {
$query_3->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
<?php
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
// the query
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Add your category ID to 'category'.
reefer these links for more information.
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
You can achieve this by alter the query of home page throught pre_get_posts hook.
Here is the code:
function txt_domain_get_distinct_post_id()
{
$post_ids = [];
//getting all non empty category
$categories = get_terms('category', array(
'orderby' => 'count',
'hide_empty' => 0,
));
foreach ($categories as $category)
{
$args = array(
'posts_per_page' => -1,
'category' => $category->term_id,
'exclude' => $post_ids,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
//getting post from a specific category
$postlist = get_posts($args);
foreach ($postlist as $post)
{
if (!in_array($post->ID, $post_ids))
{
//this will only select one post form each category
$post_ids[] = $post->ID;
break;
}
}
}
return $post_ids;
}
add_filter( 'pre_get_posts', 'txt_domain_filter_get_posts' );
function txt_domain_filter_get_posts($query)
{
if (is_home() && $query->is_main_query())
{
$post_ids = txt_domain_get_distinct_post_id();
$query->set('post__in', $post_ids);
}
return $query;
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Reference:
is_home()
get_terms
get_posts

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();
}

Wp query in meta_box breaks wp editors

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;

Resources