exclude posts without content from search result - php

I have search.php in my WordPress theme, search result is generating fine using The Loop.
What I see is some post don't have content(blank post with title only), and they still appear in search result. Which is quite obvious, but I want only to list post which has some content.
If post don't have any content it should not list in Search Result even if its title has a Search Term.
I believe you won't need to see code to understand my concern. I tried searching for this answer on all known resources, nothing found exactly.
Down vote won't help me.

Inside your search loop try adding this:
global $post;
if ( $post->post_content != '' ) {
// Display this post because it has content.
} else {
// This post has empty content so do not display it.
}

Related

why is WordPress printing my array instead of just constructing it?

I am trying to apply a custom view to all the results of a visit to a taxonomy page, which involves wrapping the whole lot of results in some DOM boilerplate and then invoking the function to display each result as an element within that boilerplate. My strategy of choice was to step through each result, populated dynamically depending which taxonomy term has led them here, and fill a bucket with those results, then display each one appropriately.
In other words, I want the WordPress loop to tell me WHICH things to display, then my own iteration to decide HOW to display them. This seemed like a simple strategy, but loops like the following appear to be always displaying my entire bucket, although I am nowhere telling it to actually print that bucket. Narrowing it down to a minimal example I have the following, which still is printing out $bucket. What is going on?
$bucket = array();
while ( have_posts() ) {
the_post();
$bucket[]=the_meta(); // this is all printed to the screen. Why?
}
the_meta(); // this is all printed to the screen. Why?
Wordpress has this concept of the_{name of function} and get_the_{name of function}. For example, the_title and get_the_title. the_title will echo out the title into the page but get_the_title will return the title instead.
the_{name of function} will echo out the result.
get_the_{name of function}, on the other hand, will return the result.
Read more on that
If you need to return the result instead of echoing it out into the page then use this version of the function: get_the_{name of function}.
So you could use the following code:
$bucket = array();
while ( have_posts() ) {
the_post();
$bucket[]=get_post_meta(get_the_ID());
}

how to bulk insert content to every post in database?

I have a WordPress website with 2000 posts.
I need all the posts content to be updated with the title of the post at the end of the content.
For example ,the Post Titles
"Google Vs Amazon",
"Mturk",
"Cotton Shirts".,etc
The contents are:
"These 2 companies....",
"Its a freelancing work...",
"Shirts are stitched with fabric..", respectively.
The respective titles are to be added at the end of each post content.
But I don't want them generated each time the post is loaded, Instead I want the actual content to be updated like that. Preferably Mysql operation.
you have not asked a clear question but anyway as I understand you need some filters to change post content.
here is a sample code to add post title to post content.
add_filter( 'the_content', 'add_post_title_to_content' );
//the function that add filter to the_content have to use one variable.
function add_post_title_to_content( $content ) {
// here we check if it is in single post or main query. so this will not apply to other positions like feeds.
if ( is_single() || in_the_loop() || is_main_query() ) {
return get_the_title().$content;
}
return $content;
}

How to write a shortcode with variable to display content from specific custom post type post?

I've only written very basic shortcodes without variables and am just not figuring out how to write one that allows someone to enter a variable to pull info from a specific post.
I have a custom post type called "Events". I want to put a shortcode on the site FrontPage that will display the contents of a specific event within that custom post type. I'm assuming a variable is the way to do this, but I don't know what the best way is. Should I be having the user find the post ID number to use as the variable? Note that I am not trying to display an arcive of the post type, only the contents of a specific post, as indicated by the shortcode variable. I can see something like the following, but don't know how to achieve it:
[display-event id="77"]
Really, this is advanced for me, so any direction you can give me would be much appreciated.
~Laura
Have a look at WordPress's documentation for add_shortcode().
You would need to add something like the following in your functions.php file:-
function baztag_func( $atts, $content = "" ) {
// What you would like your short code to do
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
add_shortcode

Insert variable into Wordpress page title for individual page

I have a photo gallery page that is using a single page in Wordpress.
The gallery display is dynamic, and relies on a URL parameter. The parameter itself is an integer of the relating wordpress page ID. (as that's the page it's about).
Here is an example of a gallery URL:
http://www.bellavou.co.uk/gallery/?pid=238
For SEO purposes, I'm trying to get away from every possible Gallery URL combination to have the page title 'Gallery', as is set in the Wordpress page settings.
I've been trying this, which has been mentioned a few times:
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
But as I'm also using Yoast SEO plugin, I think this is over-writing it (even though I uncheck 'Force title rewrite', and keep the page title field blank). Not sure why the above function doesn't seem to work.
Within the gallery page, I'm able to show the h1 title properly, passing PHP Variables into the code, but I want to do the same to the page title, but in a way that can be done directly on the page template.
Why is it that whenever I post a question, I seem to find the answer very soon afterwards?
I read a post saying that it helps to put a wp_title function before the wp_header call, which is what I tried, and that seems to work fine.
I can edit the page title just for an individual page using the PHP page template, and using this:
$procedure = isset($_GET['pid']) ? $_GET['pid'] : '';
add_filter( 'wp_title', 'wp_title_gallery', 100 );
function wp_title_gallery($title) {
global $procedure;
if(!$procedure) {
$title = the_title();
} else {
$title = the_title(get_the_title($procedure).'&nbsp');
}
return $title;
}
get_header();
$procedure gets the URL parameter, and then the function takes that ID, and gets the associated page from it, and then prints the title of the page, into the page title hook. Lastly, the WP header is called.
(Just incase anyone comes here looking for a solution)

how to display custom post type post's title on page

I am trying something out on wp. I want to display custom post types title on page post type.
here is my code.
function get_post_type_title(){
if('movie_ronny' == get_post_type()){ // if post type is movie_ronny, get post title
$movie = get_the_title(); //hold the post title in $movie.
}
if(is_page() ){ // if viewing page, display movie_ronny title
echo $movie;
}
}
add_filter('wp_head','get_post_type_title');
Above code does not display the title of movie post-type when viewing page. Any help is highly appeciated.
Your post type will most likely be shown on its own single-movie_ronny template. In which case of course you wouldn't need a conditional -
if('movie_ronny' == get_post_type()){...}
The other reason why this question is a bit strange is that your loop query must define within it's arguments what type of post, page or custom post type it is querying. So you would also then know which post type you are messing with and not need the first conditional.
And no matter what your situation is:
if(is_page() ){ // will only return true for pages. not posts or CPT's
Also,
get_the_title($id); // needs a post ID outside of the loop.
Well, there are two ways this function will fail (i.e. do nothing):
If the first if block is ignored then the second if block will not do anything (I believe it will actually show a notice, because $movie would not be defined).
If is_page() does not evaluate to true then nothing will be done in any case.
If you know anything about programming then this should be obvious. So, either you have no idea what you are doing, or I am misunderstanding something here.
Try this one:
<?php
$post_type = get_post_type_object( get_post_type($post) );
echo $post_type->label ;
?>

Categories