Community Forums › Forums › Archived Forums › General Discussion › How to create a list posts by exact keyword query ?
- This topic has 2 replies, 2 voices, and was last updated 7 years, 7 months ago by Sarah88.
-
AuthorPosts
-
August 22, 2016 at 4:26 am #191767Sarah88Member
Hi bros,
I'm using Education Pro theme to create a mini English dictionary , and i used a plugin to create these Custom Post Types : Vocabularies, Pharses, Sentences.Let's say in a detail post(Vocabularies) I want to display a list of sentences containing this word in the its content so my current code after post content of this vocabulary is:
add_action( 'genesis_entry_content', 'custom_entry_content' ); // Add custom loop function custom_entry_content() { if ( 'vocabulary' == get_post_type() || 'phrasalverbs' == get_post_type() ) { wp_reset_postdata(); $keyword=get_the_title(); $args = array( 'post_type' => 'sentence', 'posts_per_page' => 5,'s'=> $keyword); $loop = new WP_Query( $args );$dem=0; echo "<div class='ub'>List sentences</div>"; while ( $loop->have_posts() ) : $loop->the_post();$dem++; //highlight /* echo '<div class="entry-content">'; $content=get_the_content(); $keys= explode(" ",$args['s']); $content = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $content); echo $content; echo '</div>'; */ echo ' <div class="divider"><h5><span>Sentence #'.$dem.'</span></h5></div> <div class="entry-content-2">'; echo '<li class="ctk icham">'.get_the_title().'</li>'; the_content(); echo '</div>'; endwhile;
I used 's'=> $keyword to generate the list but there are some cases such as : vocabulary -> some and the sentence => "Somebody know ..." but i just want exact keyword. Could you guys tell me how to do it ? Thanks in advance.
August 23, 2016 at 5:31 am #191872Victor FontModeratorWhat you are asking is very complicated. When WordPress sees the 's'=> $keyword argument, the generated SQL includes a LIKE or IN phrase in the WHERE clause. WordPress determines whether to use LIKE or IN depending on the number of arguments passed in the keyword parameter. There is no option to make this an = (equal to).
To create an exact keyword search criteria, you have three alternatives. First, you could build a keyword table referencing back to the posts that contain the keyword. In this case you wouldn't use wp_query for the loop, but would need to build a custom loop using the $wpdb object. Second, you could use custom fields to store the keywords, in which case you would create a meta_query to extract your content. Or third, use a plugin that provides this type of functionality.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?August 31, 2016 at 1:21 am #192404Sarah88MemberHi Victor,
Your explanation is very clear to understand, thanks for that . I think you're totally right.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.