Relevanssi - Duplicate Searches logged - wordpress

I'm using (and loving) Relevanssi plugin for WordPress. Having a problem though with searches being logged twice in user searches.
Found this comment in the plugin comments about it being a problem with the template.
When digging around my search template, I just removed everything, then added things piecemeal until I had double results again.
This is the line in my search template that seems to be the trouble maker, but I don't necessarily see why.
<div style="background-image: url();"></div>
It makes 0 sense to me. If that bit of STATIC html is in the content-search.php template, I get double searches logged. If I remove it, or if all of the articles have get_the_post_thumbnail_urls, then I only get one search logged.
It seems to have something to do with style="background-image:url();" not having a value?
Here's the bigger picture:
search.php:
<?php
global $wp_query;
if ( $wp_query->found_posts > 12 ) {
$search_count = '1 - ' . $wp_query->post_count . ' of ' . $wp_query->found_posts;
} else {
$search_count = $wp_query->post_count;
}
?>
<div class="search-pg-header">
<?php include get_template_directory() . '/templates/partials/search-form.php'; ?>
<?php if ( get_search_query() !== '' ) { ?>
<h1>Showing <?php echo $search_count; ?> results for <?php echo get_search_query() ?></h1>
<?php } // end if ?>
</div>
<div class="posts-container">
<?php if ( have_posts() ):
while ( have_posts() ) : the_post();
get_template_part( 'templates/content', 'search' );
endwhile;
else: ?>
<p>There are no articles that matched your search.</p>
<?php endif; ?>
</div>
<?php if ( $wp_query->max_num_pages > 1 ) { ?>
<button>Show More</button>
<?php } // end if ?>
content-search.php:
<article data-link="<?php the_permalink(); ?>">
<div style="background-image: url(<?php the_post_thumbnail_url('card-white');?>);"></div>
<div>
<h2><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
<?php if ( has_excerpt() ) {?>
<p><?php echo get_the_excerpt(); ?></p>
<?php } //end if ?>
</div>
</article>

I did a fresh install of Wordpress and tested out this bizarre theory with 2017 theme. True enough, if I add <div style="background-image:url();"></div> to the template template-parts/post/content-excerpt.php then duplicate searches are logged.
If that div has an image: <div style="background-image:url(http://image.com);"></div> only one search is logged.
As Mikko Saari (Relevanssi) so kindly helped me with, this is actually unintended behavior from the browser.
It's definitely nice to at least have an explanation for this.
I was able to prevent this by just putting a check to see if there was a bkgd image before I rendered the inline style to the page, and now I know to be much more careful with this kind of thing in the future!
$style = ! empty( get_the_post_thumbnail_url($the_post, 'card-white') ) ?
'style="background-image: url(' . get_the_post_thumbnail_url($the_post, 'card-white') . ');"' : '';
<div class="article-card__img" <?php echo $style; ?>></div>

Related

Remove Page Header from Blog Posts

I have a page-header that is getting copied over to all of my blog posts that I want to remove completely. I am removing it visually via CSS, but SEO crawlers are still picking it up via the tag.
I am using a standard wordpress them augmented with Elementor. Here is a screenshot of the SEO report.
And here is a screenshot of the actual HTML code
Let me know if any of you have any additional questions! Thank you for your help!
You can make a conditional statement that just outputs the title of the post if on single blog posts, so something like this
<div class="container clr page-header-inner">
<?php
// Return if page header is disabled
if ( oceanwp_has_page_header_heading() ) { ?>
<!-- check to see if single blog posts -->
<?php if (is_single()) : ?>
<h1><?php echo get_the_title(); ?></h1>
<!-- otherwise show the existing title format -->
<?php else: ?>
<<?php echo esc_attr( $heading ); ?> class="page-header-title clr"<?php oceanwp_schema_markup( 'headline' ); ?>><?php echo wp_kses_post( oceanwp_title() ); ?></<?php echo esc_attr( $heading ); ?>>
<?php endif; ?>
<?php get_template_part( 'partials/page-header-subheading' ); ?>
<?php } ?>
<?php if ( function_exists( 'oceanwp_breadcrumb_trail' ) ) {
oceanwp_breadcrumb_trail();
} ?>
</div><!-- .page-header-inner -->
you would have to replace the existing code

If Data not exists - show (custom text)

I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>

Wordpress is_page_template query

i use a Plug-in on my wordpress site which uses a query to show search results, thats the code of the query:
if ( $query->have_posts() )
{
?>
<?php echo $query->found_posts; ?> Ergebnisse gefunden<br />
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<?php if ( has_post_thumbnail() ) { echo '<p>'; the_post_thumbnail("small"); echo '</p>'; } ?>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
Previous | Next
<?php
}
else
{
echo "No Results Found";
}
The search should only display results from pages who uses a specific page template (mytemplate.php)
I found the Docs http://codex.wordpress.org/Function_Reference/is_page_template who explain this, but i dont get it to work inside the above query.
any help would be nice :) thx
Please do as below.
if ( is_page_template( '{enter the name of page template with extension}' ) ) {
// Enter your if code here
} else {
// Enter your else code here.
}
For this code to work you must select the page template option while creating the page in WordPress Admin Interface.

Next / Prev post link not working wp

Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.

WordPress search only works with posts, not pages

I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/

Resources