Reputation: 4565
I want to sort category pages on my WordPress site based on a keyword. To be precise, for an example, I want to display the posts those have the keyword, let's say, "ABC" first and the other posts following them. I'm new to WordPress but have tried some ways. I've found there's a way to find posts those have a keyword in them, using,
$query = new WP_Query( 's=keyword' );
This way I can get the posts those have the keyword in them. Fine, but problem is - then I've to find those posts who don't have this keyword in them to append them to list. So is there any easy way to find out those pages who don't have the keyword "ABC" in them? Or is there any other easy way to achieve this?
Much Appreciated!
Upvotes: 0
Views: 252
Reputation: 934
Edit: Added the Boolean operator "NOT" before "LIKE" since the purpose is to catch those who do NOT HAVE the keyword
<ul>
<?php
global $wpdb;
global $post;
$str = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE `post_title` NOT LIKE '%keyword%' AND post_type = 'post' AND post_status = 'publish'";
$result = $wpdb->get_results( $str );
foreach( $result as $post ):
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink()?>"><?php the_title();?></a>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
Upvotes: 1