Images not visible in search results - php

I have this custom search function to search trough custom meta keys in Wordpress. The function works well.
However, all the images that I have added to my post using Advanced Custom Fields are now having their URL's changed to the attachment ID.
This should not happen according to the settings page in ACF:
The same field works ok on other pages, just not on the search results page. Check how it changes the image source on the search results:
How and why is the image URL here changed to the attachment ID? Kindly check out my code below:
function custom_search_function($pieces) {
// filter to select search query
if (is_search() && !is_admin()) {
global $wpdb;
$custom_fields = array('regio','provincie');
$keywords = explode(' ', get_query_var('s'));
$query = "";
foreach ($custom_fields as $field) {
foreach ($keywords as $word) {
$query .= "((mypm1.meta_key = '".$field."')";
$query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
}
}
if (!empty($query)) {
// add to where clause
$pieces['where'] = str_replace("((({$wpdb->posts}.post_title LIKE '%", "( {$query} (({$wpdb->posts}.post_title LIKE '%", $pieces['where']);
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
//$pieces['groupby'] = "{$wpdb->posts}.ID";
}
}
return ($pieces);
}
add_filter('posts_clauses', 'custom_search_function', 20, 1);
EDIT: Here's the code that displays my post results, the "foto" field is the field which is responsible for displaying the image:
<?php foreach( $posts as $post ):
//fusion-column-last, or none for normal class
$lastclass = '';
if(++$counter % 2 === 0) {
$lastclass = ' fusion-column-last';
}
setup_postdata( $post )
?>
<div class="fusion-one-half fusion-layout-column fusion-spacing-yes<?php echo $lastclass?>" style="margin-top:0px;margin-bottom:20px;background-color:white;">
<div class="fusion-column-wrapper">
<div class="bw-search-picture">
<?php $postid = get_the_ID(); ?>
<?php //echo $postid; ?>
<img src="<?php the_field('foto', $postid); ?>" alt="<?php the_title(); ?>"/>
</div>
<div class="bw-search-content">
<h2>
<a class="green" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<p class="bw-regio">Regio <?php the_field('regio'); ?></p>
<p>
<a style="color:#9C9E9F;" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">LEES VERDER ></a>
</p>
</div>
</div>
</div>
<?php endforeach; ?>

Try for the thumbnail:
$imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id( $post_id ));
echo $imgsrc[0];
Edit, use this code with the attachment id:
$imgsrc = wp_get_attachment_image_src(get_field('foto', $postid));
echo $imgsrc[0];

Rather than using
<img src="<?php the_field('foto', $postid); ?>" />
Try using
<?php $foto_url = get_field('foto', $postid); ?>
<img src="<?php echo $foto_url; ?>" />

Related

Add links to images gallery

I need to link some gallery images to different external webistes. After some research I'm not able to found a solution that isn't using a plugin. Is this possible in wordpress without using plugins? I can't install plugins for this project so any suggestion will be appreciated thanks.
Here is my code:
$args = array(
'post_type' => 'post',
'name' => 'partners'
);
$logo_img = new WP_Query( $args );
?>
<div class="container-fluid" id="">
<div class="row" style="margin-top:1em;margin-bottom:1em;">
<?php if( $logo_img->have_posts() ): while( $logo_img->have_posts() ): $logo_img->the_post();
$logo_gallery = get_post_gallery_images( $post->ID );
if( $logo_gallery ): ?>
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<?php foreach($logo_gallery as $logo ): ?>
<img class="img-fluid" src="<?php echo $logo; ?>" alt="" width="60" id="partner-logo" style="margin:0 .5em 0 .5em;"/>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
Depending on how many images we are talking about, if these images are named you could use some logic to create a URL based on the name, if they are not named and there aren't that many, you could rename them.
Here's an example;
<?php
// If there is a matching string, set a specific URL
if (preg_match('/site1/',$logo)) { $set = "yes"; $link = "https://website1.com"; }
if (preg_match('/site2/',$logo)) { $set = "yes"; $link = "https://website2.com"; }
if (preg_match('/site3/',$logo)) { $set = "yes"; $link = "https://website3.com"; }
// If there is no matching string, set a default URL
if ($set !== "yes") { $link = "https://default.com"; }
// Now encase your image in a URL
echo "<a href='$link'>
<img class='img-fluid' src='$logo' alt='' width='60' id='partner-logo' style='margin:0 .5em 0 .5em;'/>
</a>";
?>
By the way, id='partner-logo' - the id should be unique.

How to access ACF fields for a custom post type using 'wpdb'?

I have two WordPress installs, one is to be used as an intranet and the other is public. I need to pull in the data for a custom post type from the public site to be shown on the intranet, essentially so the admin doesn't need to enter the same data on each site.
So, far I've got this:
$wpdb = new wpdb(*CONNECTION DETAILS*);
$careers = $wpdb->get_results(
'SELECT *
FROM wp_posts
WHERE post_type = "career-post"'
);
if (!empty($careers)) {
echo '<ul id="careers-list">';
foreach ($careers as $career) {
echo '<li class="career">';
echo '<a class="wrap" href="http://domainname.com/career/'.$career->post_name.'" target="_blank">';
echo '<h2>'.$career->post_title.'</h2>';
echo get_field('career_duration') ? '<p class="duration">('.get_field('career_duration').')</p>' : '<p class="duration">(permanent)</p>';
echo '<h3>'.get_field('career_area').'</h3>';
echo '<p class="description">'.get_field('career_short_description').'</p>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
else {
echo '<h2>Sorry, no vacancies are currently listed.</h2>';
}
Which is pulling in the slug and title as expected, but as the ACF data is stored in a different table, I am not sure how to a) access that data and b) link it to the correct post.
Fetch ACF fields for a custom post type
<?php
$wpdb = new wpdb(*CONNECTION DETAILS*);
$careers = $wpdb->get_results(
'SELECT *
FROM wp_posts
WHERE post_type = "career-post"'
);
if (!empty($careers)) {
?>
<ul id="careers-list">
<?php
foreach ($careers as $career) {
$career_duration = get_field('career_duration', $career->ID);
$career_area = get_field('career_area', $career->ID);
$career_short_description = get_field('career_short_description', $career->ID);
?>
<li class="career">
<a href="<?php echo get_permalink($career->ID); ?>" target="_blank">
<h2><?php echo get_the_title( $career->ID ); ?></h2>
<?php
if($career_duration!="")
{
?>
<p class="duration"><?php echo $career_duration;?></p>
<?php
}
else
{
?>
<p class="duration">permanent</p>
<?php
}
?>
<h3><?php echo $career_area;?></h3>
<p class="description"><?php echo $career_short_description;?></p>
</a>
</li>
<?php
}
?>
</ul>
<?php
}
else {
echo '<h2>Sorry, no vacancies are currently listed.</h2>';
}
?>
OR
<?php
$career_post_type = 'career-post';
$career_post_args=array(
'type' => $career_post_type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$career_post_my_query = null;
$career_post_my_query = new WP_Query($career_post_args);
if( $career_post_my_query->have_posts() )
{
?>
<ul id="careers-list">
<?php
while ($career_post_my_query->have_posts()) : $career_post_my_query->the_post();
$career_duration = get_field('career_duration', $post->ID);
$career_area = get_field('career_area', $post->ID);
$career_short_description = get_field('career_short_description', $post->ID);
?>
<li class="career">
<a href="<?php echo get_permalink($post->ID); ?>" target="_blank">
<h2><?php echo get_the_title( $post->ID ); ?></h2>
<?php
if($career_duration!="")
{
?>
<p class="duration"><?php echo $career_duration;?></p>
<?php
}
else
{
?>
<p class="duration">permanent</p>
<?php
}
?>
<h3><?php echo $career_area;?></h3>
<p class="description"><?php echo $career_short_description;?></p>
</a>
</li>
<?php
endwhile;
?>
</ul>
<?php
}
else {
echo '<h2>Sorry, no vacancies are currently listed.</h2>';
}
wp_reset_query($career_post_my_query);
?>
Shital, I don't see how your second solution would work as it's not referencing the external DB?
I've tried the following (similar to your first solution) and it doesn't return anything for the ACF fields either:
foreach ($careers as $career) {
$ID = $career->ID;
$slug = $career->post_name;
$title = $career->post_title;
$duration = get_field('career_duration', $ID);
$area = get_field('career_area', $ID);
$description = get_field('career_short_description', $ID);
echo '<li class="career">';
echo '<a class="wrap" href="http://domainname.com/career/'.$slug.'" target="_blank">';
echo '<h2>'.$title.'</h2>';
echo $duration ? '<p class="duration">('.$duration.')</p>' : '<p class="duration">(permanent)</p>';
echo '<h3>'.$area.'</h3>';
echo '<p class="description">'.$description.'</p>';
echo '</a>';
echo '</li>';
}

PHP Array with only unique values

I am a beginner in coding and am trying to understand and adjust the below code to return only unique values without repetition from the database. I have tried different variations of unique_array() but without success unfortunately. How can I limit the values returned from the database to only unique ones?
<?php
$sql ="SELECT COUNT( status) AS total_building FROM add_bulding WHERE status=1";
$query = $this->db->query($sql);
$total_building = $query->row_array();
?>
<div class="b-list-section">
<?php
if(!empty($building_info))
{
foreach ($building_info as $value)
{
$id = $value['id'];
$sql = "SELECT COUNT(DISTINCT building_id) AS total_list FROM add_listing WHERE building_id='$id'";
$query = $this->db->query($sql);
$total_list = $query->row_array();
?>
<a class="blink" target="_blank" href="<?php echo base_url() . 'details_building/' . $value['id'] ?>">
<div class="bilding-wrap">
<h3 class="buildingtitle"><?php echo $value['title']; ?></h3>
<?php if(!empty($value['image'])):?>
<img class="bimage" src="<?php echo base_url("uploads/thumb/" . $value['image']); ?>" alt="no image yet">
<?php else:?>
<?php $qry12=mysql_query("SELECT * FROM add_bulding_image WHERE add_building_id=$id");
$sqr1=mysql_fetch_array($qry12); ?>
<img class="bimage" src="<?php echo base_url("uploads/thumb/" . $sqr1['image']); ?>" alt="no image yet">
<?php endif;?>
</div>
</a>
The above code returns listings that match what a user is searching for. Since 2 listings can be in the same building, the code is returning the same building twice.
Try to change your query to this:
SELECT COUNT(building_id) AS total_list FROM add_listing WHERE building_id='$id' GROUP BY building_id

Hover image description on image

I am no expert in PHP, javascript and js; so I am hoping someone can help me in easy language terms. I have just developed my first wordpress site and changed the code immensely, using this forum - thanks heaps so far!
The site: http://www.heritageglass.com.au
My request: text (description of image in media library) hovering over image.
Just like this website or this plugin, I have found several questions on this site that refer to my issue but they haven't seemed to help or I don't exactly know where to place this as it's grabbing the_title and I want the_description. Is there such a term?
PHP code in portfolio.php template:
<div data-id="post-<?php the_ID(); ?>" data-type="<?php
$categories = get_the_category();
$count = count($categories);
$i=1;
foreach($categories as $category) {
echo str_replace('-', '', $category->slug);
if ($i<$count) echo ' '; $i++;} ?>" class="post-<?php the_ID(); ?>
<?php
$categories = get_the_category();
foreach($categories as $category) {
echo str_replace('-', '', $category->slug).' '; } ?> project portfolio-item-wrap">
<div class="portfolio-item">
<?php if ( has_post_thumbnail() ) { ?>
<a class="portfolio-item-img" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'portfolio-image' ); ?></a>
<?php } ?>
<h4><?php the_title(); ?></h4>
<p>
<?php
$categories = get_the_category();
$temp = array();
foreach($categories as $category) {
if ($category->category_parent == 0) continue;
$temp[] = str_replace('-', '', $category->name);
}
echo implode(", ", $temp);
?>
</p>
</div>
</div>
I found this: Text over image using jquery and css
and added the script in the header.php. And used my .portfolio-item and .portfolio-item-img instead of .wrapper and .description - but that didn't do anything.
Hoping someone can lead me in the right direction?
Thanks!
Doing a search on the WordPress forums I found this from four years ago, not sure if it's what you need, but it might point you in the right direction.
$img_desc = $image->post_excerpt;
I think it will help you....
http://www.net-kit.com/10-jquery-caption-plugins/#
http://www.inwebson.com/jquery/jquery-image-hover-effects/
When editing a post, add two custome field, one for title, one for descritption.
Our code goes like this:
$dataType = '';
$divClass = '';
$categories = get_the_category();
$count = count($categories);
$i=1;
foreach($categories as $category) {
$dataType .= str_replace('-', '', $category->slug);
$divClass .= str_replace('-', '', $category->slug).' ';
if ($i<$count) $dataType.= ' ';
$i++;
}
$metaList = get_post_meta(get_the_ID());
$title = isset($metaList['title']) ? $metaList['title'] : '';
$description = isset($metaList['description']) ? $metaList['description'] : '';
// now we have title and description for the thumb!
?>
<div data-id="post-<?php the_ID(); ?>" data-type="<?php echo $dataType;?>"
class="post-<?php the_ID(); echo $divClass;?> project portfolio-item-wrap">
<div class="portfolio-item">
<?php if ( has_post_thumbnail() ) { ?>
<a class="portfolio-item-img" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail( 'portfolio-image' ); ?>
</a>
<?php } ?>
<h4><?php the_title(); ?></h4>
<p>
<?php
$temp = array();
foreach($categories as $category) {
if ($category->category_parent == 0) continue;
$temp[] = str_replace('-', '', $category->name);
}
echo implode(", ", $temp);
?>
</p>
</div>
</div>
I have found the answer after sooooo much research.
This is the answer: How do I get the featured image description from my wordpress page?
enter the the_post_thumbnail_caption(); within the span tags and set your span tags class in css.
done.
Thanks to all of you for your help!

Saving content from an array for a new loop

This is my first post, so I will be trying to be as thorough as possible. I am also very new to PHP.
This is a wordpress site using PODS CMS plugin so keep in mind the wordpress image uploader allows access to multiple sizes of one singular image upload. The concept is that I have a group of data called "team" and this group has three fields - images, title, bio. I will be generating a list of thumbnails for each team member in one containing unordered list and then in another containing div I will have a larger version of he image, the title, and the bio. I am basically making a tabbed content area where the thumbnails are the tabs
The ultimate HTML output would be:
<ul>
<li> <img src="thumbnailurl.jpg"/></li>
<li> <img src="thumbnailurl2.jpg"/></li>
<li> <img src="thumbnailurl3.jpg"/></li>
</ul>
<div class="panes">
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
</div>
The current issue I am having is that I can get all of the image urls for the first record, but when it comes to the second record in the second foreach i need to re-run the array for the new record but I can not figure out how.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
while ($Record->fetchRecord())
{
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
$mylist[] = array('name' => $title, 'desc' => $desc, 'id'=> $id );
?>
<ul>
<?php
foreach($image_array as $i => $image)
{
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$image_thumb_url = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$image_med_url = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$image_large_url = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$image_full_url = $image_full_url[0];
?>
<li>
<a href="<?php echo $image_large_url; ?>">
<img src="<?php echo $image_thumb_url; ?>" />
</a>
</li>
<?php } ?>
<?php } ?>
</ul>
<div class="panes">
<?php
foreach ($mylist as $person)
{ ?>
<div class="team-member" id="member<?php echo $person['id']; ?>">
<h2><?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?>
<a href="<?php echo $person['photo']; ?>">
<img src="<?php echo $person['photo']; ?>" />
</a>
<?php } ?>
</div>
</div>
Okkkay.. So i have the first problem solved!!! But it brings up a second one. I am thinking I will either need a second image field OR call just the first image in the array in the <li> and just the second image in the array for the <div>:
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
$cnt = 0;
?>
<ul class="tabs">
<?php
while ($Record->fetchRecord()) :
$mylist[$cnt] = array(
'name' => $Record->get_field('name'),
'desc' => $Record->get_field('bio'),
'id'=> $Record->get_field('id')
);
?>
<?php
$image_array = $Record->get_field('photo');
foreach($image_array as $i => $image) :
$image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
$mylist[$cnt]['img_thumb'] = $image_thumb_url[0];
$image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
$mylist[$cnt]['img_med'] = $image_med_url[0];
$image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
$mylist[$cnt]['img_large'] = $image_large_url[0];
$image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
$mylist[$cnt]['img_full'] = $image_full_url[0];
?>
<li>
<a href="#">
<img src="<?php echo $image_thumb_url[0]; ?>" />
</a>
</li>
<?php endforeach; ?>
<?php
$cnt++;
endwhile;
?> </ul>
<div class="panes">
<?php foreach ($mylist as $person) : ?>
<div class="team-member" id="member<?php echo $person['id']; ?>"><div id="member-info"><h2>Meet <?php echo $person['name']; ?></h2>
<?php echo $person['desc']; ?></div>
<a href="<?php echo $person['img_large']; ?>" rel="prettyPhoto">
<img src="<?php echo $person['img_med']; ?>" style="float:left" />
</a>
</div>
<?php endforeach; ?>
</div>
I think I can see what your problem is. Your second foreach loop doesn't benefit from the while loop, as such your only option if following this path would be to reloop through the fetched data. I wouldn't recommend doing this as there is nearly always a way you can write the code needed for the second loop into the original loop.
As an example of this I've re-written your code to incorporate the second foreach loop into the while loop (negating its need). This way, every record has a new entry into $html and $html2. Once it's looped through you have two variables that you can concat to produce the full html you are looking for.
<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
// Custom variables
$html = "<ul>\n";
$html2 = "";
$image_type=array('thumbnail','medium','large','full');
while ($Record->fetchRecord()) {
$image_array = $Record->get_field('photo');
$title = $Record->get_field('name');
$desc = $Record->get_field('bio');
$id = $Record->get_field('id');
foreach($image_array as $i => $image) {
foreach ($image_type as $type) {
$image_temp = wp_get_attachment_image_src( $image['ID'], $type , false );
$image_url[$type] = $image_temp[0];
} # End of Foreach loop
// Create List HTML in primary output variable
$html .= "<li>\n".
"<a href=\"" . $image_url['large'] ."\">\n".
"<img src=\"" . $image_url['thumbnail'] ."\" />\n".
"</a>\n".
"</li>\n";
} #End of foreach loop
// Create User HTML in secondary output variable
$html2 .= "\t<div class=\"team-member\" id=\"member" . $id . ">\n".
"<h2>" . $title . "</h2>\n".
$desc . "\n".
"<a href=\"" . $photo . ">\n". # $photo is not declared in this code (will error)
"<img src=\"" . $photo . " />\n". # as above comment
"</a>\n";
"</div>\n";
} # End of while loop
// Wrap up the list elements, concat the secondary HTML.
$html .= "</ul>\n\n".
"<div class=\"panes\">\n".
$html2 . "\n".
"</div>\n";
echo $html;
?>

Categories