Find all Wordpress posts that contain a certain word - php

Without adding any tags or categories, I need a way to generate a page that lists all Wordpress posts containing the word, for example, "design" somewhere within them. Does anyone know how to do this?

You can use WP_Query to do a search:
<?php
$query = new WP_Query( 's=keyword' );
if ($query->have_posts()){
while ( $query->have_posts() ) { $query->the_post();
echo '<h2>';
the_title();
echo '</h2>';
the_content();
} //end while
}
Let me know how it went!

Related

How to show only posts with certain category in wordpress theme?

I was wondering if there is a way to change this code to only display posts from certain category created in wordpress. Now it displays every recent post. Let's say I would create "News" category in wordpress and I want this piece of code to display only News posts.
Thanks for help
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
You can override the usual query that wordpress uses and create a custom one;
https://developer.wordpress.org/reference/classes/wp_query/
usually though for just displaying a category you can just open the category slug and provided your template has the correct archive page it will display the posts for that category.
If not use something similar to below. You can further refine your search parameters like number of posts and post types as defined in the link above.
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>

WordPress wp_query featured posts

I'm working on a WordPress site, and I'm working on a requested feature from my client. The client would like to be able to feature certain posts, so that they appear at the top of a list. To make it easy for my client, I created a featured category. So when the client wants to feature a post, all they have to do is to add the featured category to the post.
Here is my current query to display all posts with the category name of events:
$query = new WP_Query( array( 'category_name' => array('events'), 'posts_per_page' => '1' ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$postLink = get_permalink();
echo '<li class="wp-post">
<h5>'.get_the_title().'</h5>
<p>'.get_the_excerpt().'</p>
<div class="wp-post-details">
<span class="post-date">'.get_the_date().'</span>
</div>
</li>';
}
echo '<li>View All Events</li>';
wp_reset_postdata();
} else {
echo '<li>No Posts Found</li>';
}
I would like to change it, so it would display events posts that have the additional category of featured first. I have done some searching on Google and here. But as of yet, I have not found a solution that works for my instance.
I found the an solution to my issue. I just had to add if (in_category('feature')) {... before I echo the HTML.

Only showing the first sentence of a WordPress post on the front page as a "highlighted post" section

I'm currently working on my first WordPress theme and I want to create a "Highlighted Post" section on my index.php. There are a lot of scripts for this but all seem to apply a permanent filter over the entire index.php. That is problematic since i want to show the whole posts on my index.php below. Is there a way to apply this special filter only for this section and not for the whole index.php?
If you could give us more info what determines this "Highlighted" posts we might be able to be more specific in solving this.
But in the mean time I'll guess it's two latest posts which means you could have two queries in the index.php
First one would be for "Highlighted" ones:
<?php
$args = array( 'numberposts' => 2 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
<h2 class="news"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endforeach; ?>
This will give you excerpt of your post in this query.
What you could do then is limit excerpt to whatever length you'd like and add read more link if you want :)
function newExcerpt($more) {
global $post;
return '... Read more';
}
add_filter('excerpt_more', 'newExcerptReadMore');
function customExcerptLength( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'customExcerptLength', 999 );
And for other posts you could do another query but not using first two posts like this:
$count = 0;
$lastposts = get_posts();
foreach($lastposts as $post) : setup_postdata($post);
if($count > 1)
?>
<h2 class="news"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
$counter++;
<?php endforeach; ?>
This will just loop through the posts and skip first two posts.
If you have some other term or category or something else that determine Highlighted posts then you can use that in and pass it as an argument to get_posts($args).

wordpress combine page and posts

I am creating a wp template based on 2013. I want to display a 'page' that has some content from a page and then 6 posts on it. These posts have been selected via the themes options panel using the settings api. So I can get at each one using
$options = get_option( 'osc_theme_options' );
The template i have been using so far is based on a page so i am guessing I need to change the loop in someway.
The loop goes:
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...
I want to know how to alter the loop so that it pulls in just the posts that have been selected. At the moment this template/loop is only pulling in the page - which I guess is fair enough.
Would it be possible to create 'another loop' maybe under the first that then brings in the selected posts - if so how?
Many thanks
Yes you can effectively create another loop under the existing loop to display the posts. I am not sure what $options = get_option( 'osc_theme_options' ); returns, i.e an array of Post ID's etc. In order to show the posts you need to do a custom loop:
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() )
{
echo '<ul>';
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
else
{
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
This is taken from:
https://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/
Also see the following:
https://digwp.com/2011/05/loops/
http://www.smashingmagazine.com/2013/01/14/using-wp_query-wordpress/
So effectively it all boils down to the $args variable as to which posts you will get. Here is an example of multiple ids
id=2,6,17,38
So if $options = get_option( 'osc_theme_options' ); returns an array of post ids like so:
array(
0=>1
1=>22
2=>27
)
You could probably do something like:
$args = "id=".implode($options,",");
This is the best advice I can give without deeper knowledge of the theme etc.

Enter the_loop() from posts ID

I have looked for this problem and I haven't seen it asked before. I'm sorry if this is a repeated question.
I have done a complex query to my custom database tables, and I have now an array of wordpress posts IDs.
Now I would like to show them with the_loop, but I'm not able to do that. I think it's important to advise you that I'm showing the results by ajax, so all this functionality is in functions.php (maybe this information is irrelevant).
For now I'm getting the posts data with:
function imprimirResultados($resultados = null){
if ($resultados){
echo '<div class="resultados">';
foreach ($resultados as $post_id){
$post = get_post( $post_id[0], ARRAY_A );
echo '<div class="post" id="post-'.$post['ID'].'">
<h2><a href="'.$post['guid'].'" rel="bookmark" title="Permanent Link to '.$post['post_name'].'">
'.$post['post_title'].'</a></h2>
<div class="entry">
'.$post['post_excerpt'].'
</div>
</div>';
}
echo '</div>';
var_dump($post);
}else{
echo '<h2 class="center">No encontrado</h2>
<p class="center">Lo sentimos, pero la búsqueda no obtuvo resultados.</p>';
}
}
But it is not a clean method to do that, and I can't use other functionalities that the objects generated by the_loop() already have.
--------------------------------- Edit -------------------------------
I leave here the functions I use:
function resultadosLigeros_callback(){
(...)
$querystr = cargar_ligeros($marca,$modelo,$combustible,$tipo,$anio); //This function generate a MySQL query
$resultados = $wpdb->get_results($querystr, ARRAY_N);
imprimirResultados($resultados, $querystr);//This function is the one I wrote before
die();
}
You'll want to generate a new WP_Query, based on the post IDs you have. Specifically, you'll want to use post__in.
For example, since $resultados is an array:
<?php
// Fetch the posts
$query = new WP_Query( array( 'post__in' => $resultados ) );
// The Loop
while ( $query->have_posts() ) : $query->the_post();
?>
You can then use functions such as the_ID(), etc., directly within your template logic.

Categories