How do I make new posts show on map? - php

My map shows results from posts, but only old ones. Why aren't new ones showing up? I hired a coder from India but he is hiding now. (I already paid him because I didn't anticipate this bug.)
At the bottom of the map, it says: Total Geocoded Ads: 602. This is not true. There are now 608 published posts. So why aren't new ones being found?
Also, why aren't they being presented in order of post_date?
add_action('wp_ajax_query_total', 'query_total');
add_action('wp_ajax_nopriv_query_total', 'query_total');
if (defined('MULTISITE') && (MULTISITE))
{
if (!defined('TT'))
define('TT', plugins_url('/geo-ads/timthumb_wpmu.php'));
}
else
{
if (!defined('TT'))
define('TT', plugins_url('/geo-ads/timthumb.php'));
}
function query_total()
{
global $wpdb;
$table = $wpdb->prefix.'cp_ad_geocodes';
$q = "SELECT COUNT(*) FROM $table t LEFT JOIN $wpdb->posts p ON (p.ID = t.post_id) WHERE p.post_status = 'publish'";
$c = $wpdb->get_var($q);
echo $c;
die();
}
function query_geo_ads()
{
if ($_GET['range'] == 'true') {
query_geo_ads_refresh();
}
else {
global $wpdb;
$table = $wpdb->prefix.'cp_ad_geocodes';
$querystr = "SELECT t.post_id, t.category, t.lat, t.lng, p.post_title, p.post_content, m.meta_value as price, CONCAT(wpo_su.option_value, REPLACE( REPLACE( REPLACE( REPLACE(wpo.option_value,'%year%',date_format(p.post_date,'%Y')) ,'%monthnum%',date_format(p.post_date,'%m')) ,'%day%',date_format(p.post_date,'%d')) ,'%postname%',p.post_name ) ) as permalink
FROM $table t LEFT JOIN $wpdb->posts p ON (p.ID = t.post_id)
INNER JOIN $wpdb->options wpo ON wpo.option_name='permalink_structure'
INNER JOIN $wpdb->options wpo_su ON wpo_su.option_name='siteurl'
LEFT JOIN $wpdb->postmeta m ON (m.meta_key = 'cp_price') AND (m.post_id = p.ID)
WHERE p.post_status = 'publish'
AND p.post_type = 'ad_listing' ";
$ads = $wpdb->get_results($querystr);
$expires = 50000;
$cache_time = mktime();
header("Cache-Control: max-age:" . $expires . ", must-revalidate");
header("Expires: " . gmdate("D, d M Y H:i:s",$cache_time+$expires) . " GMT");
echo json_encode($ads);
die();
}
}
function get_image_url()
{ $post_id = intval($_POST['id']);
$attachment = get_posts(array('post_type' => 'attachment','post_status' => 'publish','order' => 'ASC','orderby' => 'post_date','post_mime_type' => 'image','post_parent' => $post_id));
$height = 100;
$width = 100;
$url = TT.'?src='.wp_get_attachment_url($attachment[0]->ID, false).'&h='.$height.'&w='.$width;
if ($attachment)
echo $url;
die();
}
function query_geo_ads_refresh() {
?>
<div class="undertab"><span class="big">Classified Ads / <strong><span class="colour">Just Listed</span></strong></span></div>
<?php
global $wpdb,$wp_query, $post;
$exclude_categories = $_REQUEST['exclude_categories'];
$term_id = $_REQUEST['term_id'];
$term_slug = $_REQUEST['term_slug'];
$taxonomy_name = $_REQUEST['taxonomy_name'];
$northeast_lng = $_REQUEST['northeast_lng'];
$northeast_lat = $_REQUEST['northeast_lat'];
$southwest_lng = $_REQUEST['southwest_lng'];
$southwest_lat = $_REQUEST['southwest_lat'];
if(!empty($exclude_categories)) {
$exclude_categories = explode(';', $exclude_categories);
}
if(!$term_id) // this means root
//$term_categories = get_terms( 'ad_cat', 'orderby=count&hide_empty=0' );
$term_categories = get_terms( 'ad_cat', 'order=DESC','orderby=post_date' );
else {
$current_term = get_term_by( 'slug', $term_slug, $taxonomy_name );
$term_categories = get_terms( $taxonomy_name, 'order=DESC','orderby=post_date' );
if(empty($term_categories))
$term_categories[] = $current_term;
}
foreach ($term_categories as $catinfo_obj)
{
$term_id = $catinfo_obj->term_id;
$arrsrch = array("'");
$arrrep = array('');
$name = htmlspecialchars_decode($catinfo_obj->name);
$name = strtolower(str_replace($arrsrch,$arrrep,$name));
if(!empty($exclude_categories)) {
if(in_array(strtolower($name), $exclude_categories))
continue;
}
$subterm_ids = get_term_children( $term_id, 'ad_cat' );
$subterm_ids[] = $term_id;
$term_ids = implode(',', $subterm_ids);
if($term_id)
{
$my_post_type = "'ad_listing'";
$sql = "select * from $wpdb->posts p where p.post_type in ($my_post_type) and p.post_status in ('publish') and p.ID in (select tr.object_id from $wpdb->term_relationships tr join $wpdb->term_taxonomy t on t.term_taxonomy_id=tr.term_taxonomy_id where t.term_id in ($term_ids) )";
$postinfo = $wpdb->get_results($sql);
$data_arr = array();
$geo_codes = array();
if($postinfo)
{
$srcharr = array("'");
$replarr = array("\'");
foreach($postinfo as $postinfo_obj)
{
$ID = $postinfo_obj->ID;
$title = str_replace($srcharr,$replarr,($postinfo_obj->post_title));
$plink = get_permalink($postinfo_obj->ID);
$geocode = cp_get_geocode($ID);
$lat = $geocode['lat'];
$lng = $geocode['lng'];
// check current post geo place is within map coord area
if($lat > $northeast_lat || $lat < $southwest_lat)
continue;
if($northeast_lng > $southwest_lng) {
if($lng > $northeast_lng || $lng < $southwest_lng)
continue;
} else {
if($lng > 0 && $lng < $southwest_lng)
continue;
if($lng < 0 && $lng > $northeast_lng)
continue;
}
$address = get_post_meta($ID,'cp_address_line',true);
if(!empty($address))
$address = str_replace($srcharr,$replarr,(get_post_meta($ID,'cp_address_line',true)));
else {
$street = get_post_meta($ID,'cp_street',true);
$city = get_post_meta($ID,'cp_city',true);
$state = get_post_meta($ID,'cp_state',true);
$country = get_post_meta($ID,'cp_country',true);
if(!empty($street))
$address .= $street . ', ';
if(!empty($city))
$address .= $city . ', ';
if(!empty($state))
$address .= $state . ', ';
if(!empty($country))
$address .= $country . ', ';
$address = trim($address);
$address = substr($address, 0, strlen($address)-1);
$address = str_replace($srcharr,$replarr,$address);
}
$phone = str_replace($srcharr,$replarr,(get_post_meta($ID,'cp_phone',true)));
$email = str_replace($srcharr,$replarr,(get_post_meta($ID,'cp_email',true)));
$author = get_userdata($postinfo_obj->post_author);
$category = get_the_category($postinfo_obj->ID);
if($postinfo_obj->post_type == 'ad_listing') {
$timing = date('M d, Y',strtotime(get_post_meta($ID,'cp_start_date',true))).' to '.date('M d, Y',strtotime(get_post_meta($ID,'cp_end_date',true)));
} else {
$timing = date('M d, Y',strtotime(get_post_meta($ID,'st_date',true))).' to '.date('M d, Y',strtotime(get_post_meta($ID,'end_date',true))).'<br />'.get_post_meta($ID,'st_time',true).' to '.get_post_meta($ID,'end_time',true);
}
?>
<?php appthemes_before_post(); ?>
<div class="post-block-out">
<div class="post-block">
<div class="post-left">
<?php if ( get_option('cp_ad_images') == 'yes' ) cp_ad_loop_thumbnail($ID); ?>
</div>
<div class="<?php if ( get_option('cp_ad_images') == 'yes' ) echo 'post-right'; else echo 'post-right-no-img'; ?> <?php echo get_option('cp_ad_right_class'); ?>">
<!-- <h3><?php if ( mb_strlen( $title ) >= 75 ) echo mb_substr( $title, 0, 75 ).'...'; else echo $title; ?></h3>
<div class="clr"></div>
<p class="post-meta">
<span class="folder">
<a href="/ad-category/<?php echo $catinfo_obj->slug; ?>/">
<?php echo $name; ?>
</a>
</span> |
<span class="owner">
<a href="/author/<?php echo $author->user_nicename; ?>">
<?php echo $author->display_name; ?>
</a>
</span> |
<span class="clock"><span><?php echo $timing; ?></span></span>
</p>-->
</div>
<div class="clr"></div>
</div><!-- /post-block -->
</div><!-- /post-block-out -->
<?php
}
}
}
}
die();
}
?>
This is the index.php home page file. This is how the home page appears:
<?php get_header(); ?>
<div class="content_botbg">
<div class="content_res">
<?php // Comment the default CP Slider
// if ( file_exists(STYLESHEETPATH . '/featured.php') )
// include_once(STYLESHEETPATH . '/featured.php');
// else
// include_once(TEMPLATEPATH . '/featured.php');
?>
<!-- left block -->
<!--<div class="content_left">-->
<?php if ( get_option('cp_home_layout') == 'directory' ) : ?>
<div class="shadowblock_out">
<div class="shadowblock">
<h2 class="dotted"><?php _e('Ad Categories','appthemes')?></h2>
<div id="directory" class="directory twoCol">
<?php echo cp_create_categories_list( 'dir' ); ?>
<div class="clr"></div>
</div><!--/directory-->
</div><!-- /shadowblock -->
</div><!-- /shadowblock_out -->
<?php endif; ?>
<div class="tabcontrol">
<ul class="tabnavig">
<li><span class="big"><?php _e('Just Listed','appthemes')?></span></li>
<li><span class="big"><?php _e('Most Popular','appthemes')?></span></li>
<li><span class="big"><?php _e('Random','appthemes')?></span></li>
</ul>
<?php remove_action( 'appthemes_after_endwhile', 'cp_do_pagination' ); ?>
<?php $post_type_url = get_bloginfo('url').'/'.get_option('cp_post_type_permalink').'/'; ?>
<!-- tab 1 -->
<div id="block1">
<div class="clr"></div>
<div class="undertab"><span class="big"><?php _e('Classified Ads','appthemes') ?> / <strong><span class="colour"><?php _e('Just Listed','appthemes') ?></span></strong></span></div>
<?php
// show all ads but make sure the sticky featured ads don't show up first
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array('post_type' => APP_POST_TYPE, 'ignore_sticky_posts' => 1, 'paged' => $paged) );
global $wp_query;
$total_pages = max( 1, absint( $wp_query->max_num_pages ) );
?>
<?php //get_template_part( 'loop', 'ad_listing' ); ?>
<?php
if( $total_pages > 1 ){ ?>
<div class="paging"> <?php _e( 'View More Ads', 'appthemes' ); ?> </div>
<?php } ?>
</div><!-- /block1 -->
<!-- tab 2 -->
<div id="block2">
<div class="clr"></div>
<div class="undertab"><span class="big"><?php _e('Classified Ads','appthemes') ?> / <strong><span class="colour"><?php _e('Most Popular','appthemes') ?></span></strong></span></div>
<?php get_template_part( 'loop', 'featured' ); ?>
<?php global $cp_has_next_page; ?>
<?php if($cp_has_next_page){ ?>
<div class="paging"> <?php _e( 'View More Ads', 'appthemes' ); ?> </div>
<?php } ?>
<?php wp_reset_query(); ?>
</div><!-- /block2 -->
<!-- tab 3 -->
<div id="block3">
<div class="clr"></div>
<div class="undertab"><span class="big"><?php _e('Classified Ads','appthemes') ?> / <strong><span class="colour"><?php _e('Random','appthemes') ?></span></strong></span></div>
<?php
// show all random ads but make sure the sticky featured ads don't show up first
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array('post_type' => APP_POST_TYPE, 'ignore_sticky_posts' => 1, 'paged' => $paged, 'orderby' => 'rand') );
global $wp_query;
$total_pages = max( 1, absint( $wp_query->max_num_pages ) );
?>
<?php get_template_part( 'loop', 'ad_listing' ); ?>
<?php
if( $total_pages > 1 ){ ?>
<div class="paging"> <?php _e( 'View More Ads', 'appthemes' ); ?> </div>
<?php } ?>
</div><!-- /block3 -->
</div><!-- /tabcontrol -->
</div><!-- /content_left -->
<?php //get_sidebar(); ?>
<div class="clr"></div>
</div><!-- /content_res -->
</div><!-- /content_botbg -->

Related

Merging and ordering XML feeds with PHP - Very slow to load

Within wordpress and using ACF Pro, I'm merging multiple Songkick XML feeds with PHP, attaching an artist name to them (annoyingly each feed doesn't include the artist name), and ordering them by the event date.
I've managed to put this all together (with help from different questions on here) using separate steps, but the page is loading very slowly, so I wondered if there was a way of streamlining or merging some of the steps?
With the code below I am:
Fetching multiple XML feeds (sing an ACF fields from other pages on the site)
Attaching an artist name to each feed
Merging the feeds (whilst removing some of the data to try and speed
up the processing time)
Outputting the information into a table while ordering by date
My code below:
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => 'artists'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : $artistFeedCount = 0; while ( $parent->have_posts() ) : $parent->the_post(); if( get_page_template_slug() == 'template-artist.php' && 'publish' === get_post_status() ) {
$singleArtistName = get_the_title();
$singleArtistSongkickRSS = 'https://api.songkick.com/api/3.0/artists/' . get_field('artist_songkick_id') . '/calendar.xml?apikey=XXXXXXXXXXXXXXXX';
$SongkickEvents[]=$singleArtistName;
$SongkickEvents[$singleArtistSongkickRSS] = $SongkickEvents[$artistFeedCount];
unset($SongkickEvents[$artistFeedCount]);
$artistFeedCount++;
?>
<?php }; endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
<?php
$eventsDom = new DOMDocument();
$eventsDom->appendChild($eventsDom->createElement('events'));
foreach ($SongkickEvents as $artist_dates => $artist_name ) {
$eventsAddDom = new DOMDocument();
$eventsAddDom->load($artist_dates);
$events = $eventsAddDom->getElementsByTagName('event');
if ($eventsAddDom->documentElement) {
foreach ($events as $event) {
$eventsDom->documentElement->appendChild(
$eventsDom->importNode($event, TRUE)
);
$artistName = $eventsDom->createElement('mainartist', $artist_name);
foreach($eventsDom->getElementsByTagName('event') as $singleEvent) {
$singleEvent->appendChild($artistName);
}
foreach($eventsDom->getElementsByTagName('performance') as $singlePerformance) {
$singlePerformance->parentNode->removeChild($singlePerformance);
}
}
}
}
$newXML = $eventsDom->saveXml();
$LiveDates = simplexml_load_string($newXML);
$eventsArr=array();
foreach($LiveDates->event as $eventsArrSingle)
{
$eventsArr[]=$eventsArrSingle;
}
usort($eventsArr,function($dstart,$dend){
return strtotime($dstart->start['date'])-strtotime($dend->start['date']);
});
foreach($eventsArr as $eventsArrSingle) { ?>
<div class="event-row <?php $eventStatus = $eventsArrSingle['status']; if($eventStatus == 'cancelled' || $eventStatus == 'postponed'): echo 'cancelled'; endif; ?>">
<div class="event-block event-date">
<span><?php $eventDate=$eventsArrSingle->start['date']; echo date("d", strtotime($eventDate)); ?></span>
<?php echo date("M", strtotime($eventDate)); ?>
</div>
<div class="event-block event-info">
<span><?php echo $eventsArrSingle->mainartist; ?></span>
<?php if($eventsArrSingle->venue['displayName'] != 'Unknown venue'): echo $eventsArrSingle->venue['displayName'] . ', '; endif; ?><?php echo $eventsArrSingle->venue->metroArea['displayName']; ?>
</div>
<div class="event-block event-button">
<span><?php if($eventStatus == 'cancelled'): echo 'Cancelled'; elseif($eventStatus == 'postponed'): echo 'Postponed'; else: echo 'Tickets'; endif; ?></span> <i class="fas fa-arrow-right"></i>
</div>
</div>
<?php };?>
Any help would be greatly appreciated, I'm sure there's a way of merging everything in fewer steps!
For anyone coming across the same issue, this is the solution I used. Using the Transient API to store the feed for 3 hours at a time.
<?php
function eventsFunction() {
// Do we have this information in our transients already?
$eventTransient = get_transient( 'eventsTransientData' );
// Yep! Just return it and we're done.
if( ! empty( $eventTransient ) ) {
echo $eventTransient;
} else {
$single_artist_ids = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => 'artists'
);
$parent = new WP_Query( $single_artist_ids );
if ( $parent->have_posts() ) : $artistFeedCount = 0; while ( $parent->have_posts() ) : $parent->the_post(); if( get_page_template_slug() == 'template-artist.php' && 'publish' === get_post_status() ) {
$singleArtistName = get_the_title();
$singleArtistSongkickRSS = 'https://api.songkick.com/api/3.0/artists/' . get_field('artist_songkick_id') . '/calendar.xml?apikey= XXXXXXXXXXXXXXXX';
$SongkickEvents[]=$singleArtistName;
$SongkickEvents[$singleArtistSongkickRSS] = $SongkickEvents[$artistFeedCount];
unset($SongkickEvents[$artistFeedCount]);
$artistFeedCount++;
?>
<?php }; endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
<?php
$eventsDom = new DOMDocument();
$eventsDom->appendChild($eventsDom->createElement('events'));
foreach ($SongkickEvents as $artist_dates => $artist_name ) {
$eventsAddDom = new DOMDocument();
$eventsAddDom->load($artist_dates);
$events = $eventsAddDom->getElementsByTagName('event');
if ($eventsAddDom->documentElement) {
foreach ($events as $event) {
$eventsDom->documentElement->appendChild(
$eventsDom->importNode($event, TRUE)
);
$artistName = $eventsDom->createElement('mainartist', $artist_name);
foreach($eventsDom->getElementsByTagName('event') as $singleEvent) {
$singleEvent->appendChild($artistName);
}
foreach($eventsDom->getElementsByTagName('performance') as $singlePerformance) {
$singlePerformance->parentNode->removeChild($singlePerformance);
}
}
}
}
$eventsXML = $eventsDom->saveXml();
$LiveDates = simplexml_load_string($eventsXML);
$eventsArr=array();
foreach($LiveDates->event as $eventsArrSingle)
{
$eventsArr[]=$eventsArrSingle;
}
usort($eventsArr,function($dstart,$dend){
return strtotime($dstart->start['date'])-strtotime($dend->start['date']);
});
$eventsStored = '';
foreach($eventsArr as $eventsArrSingle) {
$eventsStored .= '<div class="event-row ';
$eventStatus = $eventsArrSingle['status']; if($eventStatus == 'cancelled' || $eventStatus == 'postponed'): $eventsStored .= 'cancelled'; endif;
$eventsStored .= '">
<div class="event-block event-date">
<span>';
$eventDate=$eventsArrSingle->start['date']; $eventsStored .= date("d", strtotime($eventDate));
$eventsStored .= '</span>';
$eventsStored .= date("M", strtotime($eventDate));
$eventsStored .= '</div>
<div class="event-block event-info">
<span>';
$eventsStored .= $eventsArrSingle->mainartist;
$eventsStored .= '</span>';
if($eventsArrSingle->venue['displayName'] != 'Unknown venue'): $eventsStored .= $eventsArrSingle->venue['displayName'] . ', '; endif;
$eventsStored .= $eventsArrSingle->venue->metroArea['displayName'];
$eventsStored .= '</div>
<div class="event-block event-button">
<a href="' . $eventsArrSingle['uri'] . '" target="_blank"><span>';
if($eventStatus == 'cancelled'): $eventsStored .= 'Cancelled'; elseif($eventStatus == 'postponed'): $eventsStored .= 'Postponed'; else: $eventsStored .= 'Tickets'; endif;
$eventsStored .= '</span> <i class="fas fa-arrow-right"></i></a>
</div>
</div>';
};
set_transient( 'eventsTransientData', $eventsStored, 3*HOUR_IN_SECONDS );
echo $eventsStored;
}
}
eventsFunction();
?>

wordpress get_terms and WP_Query not working as expected

i've created a colophon for my website in which i post all the logos of the different sponsors i have. I add all the sponsors via custom post type. i also added a specific custom taxonomy to distinguish between the different typologies of sponsorships.
I use this code in the footer.php to display them:
<?php $terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { ?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $term->slug,
);
$pesca_post = new WP_Query ($arg);
$quanti_post = $pesca_post->post_count;
if(have_posts()){
while ($pesca_post->have_posts()) : $pesca_post->the_post();
$featured = get_the_post_thumbnail_url(get_the_ID(),'large');
if ($quanti_post == 5){
$classe_bootstrap = 15;
}elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
echo '<div class="col-md-' . $classe_bootstrap . '">';
if (isset($featured)){
$img = $featured;
}else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($post->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
<?php echo '</div>';
endwhile;
}?>
</div>
<?php }
}?>
my problem is that this code is completely working just on some pages, on other it shows the contents avoiding the ones belonging to the first term, no matter which it will be.
I have noticed that it works in pagaes where i use other queries.
What am i doing wrong?
i changed it in this way and now it's working!
$terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { //per ogni termine presente
$nome = $term->slug;?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $nome,
);
$elementi = get_posts($arg);
$quanti_post = count( $elementi );
if ($quanti_post == 5){
$classe_bootstrap = 15;
}
elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}
elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
foreach($elementi as $elemento){
$featured = get_the_post_thumbnail_url($elemento->ID,'large');
if (isset($featured)){
$img = $featured;
}
else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<div class="col-md-<?php echo $classe_bootstrap; ?>">
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($elemento->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
</div>
<?php }?>
</div>
<?php }
}?>

Wordpress: get_post_field() throws error like it's trying to redefine a function

I have a function called getNews().
I call getNews() like this:
<div class="wrapper" id="blog">
<div class="blogPosts">
<?PHP getNews($postPerPage, 0, $theme_link, $pageNum); ?>
</div>
</div>
The function works until it tries to get the article text from wordpress using get_post_field().
function getNews($numPosts, $offset, $theme_link, $pageNum) {
$offset = $numPosts * $pageNum;
$recent_args = array('numberposts' => $numPosts, 'offset' => $offset,);
$articles = wp_get_recent_posts($recent_args);
foreach($articles as $article){
$article_id = $article['ID'];
...
echo $article_text = get_post_field('post_content', $article_id);
...
}
}
When I run this, I get the error saying Fatal error: Cannot redeclare getNews() (previously declared in /homepages/30/d545089862/htdocs/VintageTroubleMusic/wp-content/plugins/enhanced-text-widget/enhanced-text-widget.php(57)
For some reason, it's running my loop twice.
Any advice?
EDIT
I'm including the full code here: This is written inside of an "Enhanced Text widget" inside of a page.
<?PHP
$theme_link = "/wp-content/themes/thestory-child/";
$pageNum = $_GET['pa'];
$postPerPage = 27;
$qry_numPosts = mysql_query("SELECT * FROM Ykcfdlqhposts WHERE post_status = 'publish' AND post_type = 'post'") or die(mysql_error());
$numPosts = mysql_num_rows($qry_numPosts);
$numPages = ceil($numPosts/$postPerPage) . " pages";
if(!$pageNum){
$pageNum = 0;
}
?>
<!-- START OF THE BODY HERE -->
<h3 class="widget-title" style="float:left;">Headlines</h3>
<div class="wrapper" id="paginate1">
<?PHP paginate($numPosts, $numPages); ?>
</div>
<div class="wrapper" id="blog">
<div class="blogPosts">
<?PHP getNews($postPerPage, 0, $theme_link, $pageNum); ?>
</div>
</div>
<div class="wrapper" id="paginate2">
<?PHP paginate($numPosts, $numPages); ?>
</div>
And inside of the functions.php page
function getNews($numPosts, $offset, $theme_link, $pageNum) {
$offset = $numPosts * $pageNum;
$recent_args = array('numberposts' => $numPosts, 'offset' => $offset,);
$articles = wp_get_recent_posts($recent_args);
foreach($articles as $article){
$article_id = $article['ID'];
$article_link = get_permalink($article_id);
$article_length = 350;
$article_background = get_the_post_thumbnail($article_id);
$article_title = get_post_meta( $article_id, 'short_title', true );
$article_text = "test";
$article_text = get_post_field('post_content', $article_id);
//$article_text = get_post_field('post_content', $article_id);
//$article_text = strip_tags($article_text);
//$article_text_length = strlen($article_text);
/*
if($article_text_length >= $article_length) {
$article_text_pos = strpos($article_text, " ", $article_length);
$article_text = substr($article_text, 0, $article_text_pos)."... ";
}
*/
?>
<div class="article">
<?PHP //Get the article image
if (!$article_background) {
$rand = rand(1, 5);
$img = $theme_link . "images/img" . $rand . ".jpg";
} else {
$img = explode('src="', $article_background);
$img = explode(".jpg", $img[1]);
$img = $img[0] . ".jpg";
}?>
<a href="<?= $article_link; ?>">
<div class="article__image" style="background-image: url('<?= $img; ?>');"></div>
<div class="article__title"><?= $article_title; ?></div>
</a>
<div class="article__details">
<div class="article-text">
<?= $article_text; ?>
</div>
READ MORE
</div>
</div>
<? }
}
function paginate($numPosts, $numPages) {
?>
<div class="page">
<span>Page:</span><?PHP
$i = 0;
while($i < $numPages){
$i++;
if($i == $pageNum){
echo '<span style="background-color:transparent; text-decoration:underline; color:white; cursor:default;">'.$i.'</span>';
} else {
echo "<a href='?pa=".$i."'><span>".$i."</span></a>";
}
}
?>
</div>
<?
}

conditional sidebar for certain post id Wordpress PHP

I'm new to PHP and and using a Wordpress one-page theme where each section has a unique post ID. I want to add a sidebar only to a post with a specific ID. The code for that specific ID should add a few divs for styling as well as the sidebar. I've tried adding a simple if/else statement and putting the code with the extra divs and sidebar in the if, and the original code without the sidebar or extra styling in the else but my code results not only styling for the widget on each post, but also removes the other content from the post I'm targeting. This is the entire php file for the template. Any help is much appreciated!
<?php
/*
Template Name: Onepage
*/
?>
<?php global $smof_data; ?>
<?php get_header();?>
<?php
if ( ( $locations = get_nav_menu_locations() ) && $locations['primary'] && !disable_onepage() ) {
$menu = wp_get_nav_menu_object( $locations['primary'] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$pages_included = array();
foreach ( $menu_items as $item ):
if ($item->object != 'page') continue;
$menu_state = get_post_meta($item->ID,'menu-item-menu-state',true);
if ($menu_state == 'link') continue;
$pages_included[] = $item->object_id;
endforeach;
} else {
$pages_included[] = array();
}
$count = 0;
/* IF THERE IS AT LEAST ONE PAGE MENU ITEM */
if (!empty($pages_included)) :
$args = array(
'post_type' => 'page',
'post__in' => $pages_included,
'posts_per_page' => count($pages_included),
'orderby' => 'post__in',
);
$onepage_query = new WP_Query();
$onepage_query->query($args);
endif;
if ( $onepage_query->have_posts() ): while( $onepage_query->have_posts() ): $onepage_query->the_post(); $count++;?>
<?php // Some PHP stuffs
$page_template = get_post_meta(get_the_ID(),'_wp_page_template',true);
$page_template = basename($page_template,'.php');
/* ----------------------------- GET OPTIONS ----------------------------------- */
/* Title Options
----------------------------------------------- */
$subtitle = get_post_meta(get_the_ID(),'_wi_subtitle',true);
$subtitle = trim($subtitle);
$title_image = false;
$title_image_ids = get_post_meta(get_the_ID(),'_wi_title-image',false);
$title_image_id = '';
foreach ( $title_image_ids as $tii ) {
if ( !wp_get_attachment_image_src( $tii ) ) continue;
$title_image_id = $tii;
}
if ( $title_image_id ) {
$title_image = wp_get_attachment_image_src($title_image_id,'full');
$title_image = $title_image[0];
}
$hide_title = get_post_meta(get_the_ID(),'_wi_hide-title-area',true);
/* Page Background Options
----------------------------------------------- */
$page_background_type = get_post_meta(get_the_ID(),'_wi_page-background-type',true);
$page_background_color = get_post_meta(get_the_ID(),'_wi_page-background-color',true);
$page_background_image = get_post_meta(get_the_ID(),'_wi_page-background-image',true);
$page_background_image_size = get_post_meta(get_the_ID(),'_wi_page-background-image-size',true);
$page_background_image_position = get_post_meta(get_the_ID(),'_wi_page-background-image-position',true);
$page_background_pattern = get_post_meta(get_the_ID(),'_wi_page-background-pattern',true);
$page_background_pattern_retina = get_post_meta(get_the_ID(),'_wi_page-background-pattern-retina',true);
/* Separator Background Options
----------------------------------------------- */
$disable_separator = get_post_meta(get_the_ID(),'_wi_disable-page-separator',true);
$separator_content = get_post_meta(get_the_ID(),'_wi_separator-content',true);
$background_or_pattern = get_post_meta(get_the_ID(),'_wi_background-image-or-pattern',true);
if ( $background_or_pattern!='pattern' ) $background_or_pattern = 'background';
$background_images = get_post_meta(get_the_ID(),'_wi_background-image',false);
$background_image = '';
foreach ( $background_images as $bgim ) {
if ( !wp_get_attachment_image_src( $bgim) ) continue;
$background_image = $bgim;
}
$overlay_opacity = get_post_meta(get_the_ID(),'_wi_overlay-opacity',true);
$overlay_opacity = absint($overlay_opacity);
$clipmask_opacity = get_post_meta(get_the_ID(),'_wi_clipmask-opacity',true);
$clipmask_opacity = absint($clipmask_opacity);
$enable_parallax = get_post_meta(get_the_ID(),'_wi_enable-parallax-effect',true);
$parallax_class = ( $enable_parallax ) ? ' parallax' : '';
$padding = get_post_meta(get_the_ID(),'_wi_padding-top-bottom',true);
$predefined_pattern = get_post_meta(get_the_ID(),'_wi_predefined-pattern',true);
$retina_custom_patterns = get_post_meta(get_the_ID(),'_wi_retina-custom-pattern',false);
$retina_custom_pattern = '';
foreach ($retina_custom_patterns as $bgim ) {
if ( !wp_get_attachment_image_src( $bgim) ) continue;
$retina_custom_pattern = $bgim;
}
$custom_patterns = get_post_meta(get_the_ID(),'_wi_custom-pattern',false);
$custom_pattern = '';
foreach ( $custom_patterns as $bgim ) {
if ( !wp_get_attachment_image_src( $bgim) ) continue;
$custom_pattern = $bgim;
}
if ( $background_or_pattern != 'pattern' ) {
$background_type = 'image';
if ( $background_image ) {
$background_image = wp_get_attachment_image_src($background_image,'full');
$background_image = $background_image[0];
$background_size = 'cover';
} else {
$background_image = '';
$background_size = '';
}
$retina_background_image = $background_image;
} else { // $background_or_pattern == 'pattern'
$background_type = 'pattern';
if ( $custom_pattern ) {
$pattern = $custom_pattern;
if ( $retina_custom_pattern ) {
$retina_pattern = $retina_custom_pattern;
} else {
$retina_pattern = $custom_pattern;
}
$pattern = wp_get_attachment_image_src($pattern,'full');
$pattern = $pattern[0];
$retina_pattern = wp_get_attachment_image_src($retina_pattern,'full');
$retina_pattern = $retina_pattern[0];
} else {
$pattern = $predefined_pattern;
$retina_pattern = str_replace( '.png' , '_#2X.png' , $pattern );
}
if ( !$pattern ) $pattern = get_template_directory_uri().'/images/sidrbg/argyle.png';
if ( $pattern ) {
$background_image = $pattern;
$background_size = (array) #getimagesize($pattern);
if ( isset($background_size[0]) && isset($background_size[1]) ) {
$background_size = $background_size[0] . 'px ' . $background_size[1] . 'px';
} else {
$background_size = 'auto';
}
} else {
$background_image = '';
$background_size = '';
}
if ( $retina_pattern ) {
$retina_background_image = $retina_pattern;
} else {
$retina_background_image = $pattern;
}
} // image or pattern
?>
<?php if ( !$disable_separator ) :?>
<style type="text/css">
#page-separator-<?php echo esc_html($post->post_name);?> {
background-image:url(<?php echo esc_url($background_image);?>);
-webkit-background-size:<?php echo esc_html($background_size);?>;
-moz-background-size:<?php echo esc_html($background_size);?>;
background-size:<?php echo esc_html($background_size);?>;
padding-top:<?php echo absint($padding);?>px;
padding-bottom:<?php echo absint($padding);?>px;
}
#page-separator-<?php echo esc_html($post->post_name);?> .overlay {
opacity:<?php echo ($overlay_opacity/100);?>;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=<?php echo ($overlay_opacity);?>)";
filter: alpha(opacity=<?php echo ($overlay_opacity);?>);
}
#page-separator-<?php echo esc_html($post->post_name);?> .clipmask {
opacity:<?php echo ($clipmask_opacity/100);?>;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=<?php echo ($clipmask_opacity);?>)";
filter: alpha(opacity=<?php echo ($clipmask_opacity);?>);
}
#media (-webkit-min-device-pixel-ratio: 1.25), (min-resolution: 120dpi){
#page-separator-<?php echo esc_html($post->post_name);?> {
background-image:url(<?php echo esc_url($retina_background_image);?>);
}
}
</style>
<div class="<?php echo esc_attr('page-separator background-type-' . $background_type . $parallax_class);?>" id="<?php echo esc_attr('page-separator-' . $post->post_name);?>">
<div class="overlay"></div>
<div class="clipmask"></div>
<div class="container">
<div class="content">
<?php echo do_shortcode($separator_content); ?>
</div><!-- .content -->
</div><!-- .container -->
</div><!-- .page-separator #page-separator-$post->post_name -->
<?php endif; // enable/disable page separator ?>
<style type="text/css">
<?php if($page_background_color){?>
.wi-page#<?php echo esc_html($post->post_name);?> {
background-color:<?php echo sanitize_hex_color($page_background_color);?>;
}
<?php } ?>
/* --------- BACKGROUND PATTERN OPTIONS ------------ */
<?php if ($page_background_type == 'pattern'): ?>
<?php if($page_background_pattern){
$pattern = wp_get_attachment_image_src($page_background_pattern,'full');
$pattern = $pattern[0];
$background_size = (array) #getimagesize($pattern);
$background_size = $background_size[0] . 'px ' . $background_size[1] . 'px';
?>
.wi-page#<?php echo esc_html($post->post_name);?> {
background-image:url(<?php echo esc_url($pattern);?>);
-webkit-background-size:<?php echo esc_html($background_size);?>;
-moz-background-size:<?php echo esc_html($background_size);?>;
background-size:<?php echo esc_html($background_size);?>;
}
<?php } ?>
<?php if($page_background_pattern_retina){
$pattern = wp_get_attachment_image_src($page_background_pattern_retina,'full');
$pattern = $pattern[0];
?>
#media (-webkit-min-device-pixel-ratio: 1.25), (min-resolution: 120dpi){
.wi-page#<?php echo esc_html($post->post_name);?> {
background-image:url(<?php echo esc_url($pattern);?>);
}
}
<?php } ?>
/* --------- BACKGROUND IMAGE OPTIONS ------------ */
<?php elseif ($page_background_type == 'image'): ?>
<?php if($page_background_image){
$page_background_image = wp_get_attachment_image_src($page_background_image,'full');
$page_background_image = $page_background_image[0];
?>
.wi-page#<?php echo esc_html($post->post_name);?> {
background-image:url(<?php echo esc_url($page_background_image);?>);
-webkit-background-size:<?php echo esc_html($page_background_image_size);?>;
-moz-background-size:<?php echo esc_html($page_background_image_size);?>;
background-size:<?php echo esc_html($page_background_image_size);?>;
background-position:<?php echo esc_html($page_background_image_position);?>;
}
<?php } ?>
<?php endif; // page background type ?>
</style>
<?php
if ($post->ID == '4048') {
?>
<div <?php post_class('wi-page '.$page_template);?> id="<?php echo esc_attr($post->post_name);?>">
<?php if ( !$hide_title ){ ?>
<!-- TITLE -->
<div class="title-area">
<div class="container">
<div class="pad">
<?php if ( $title_image ) : ?>
<div class="image">
<img src="<?php echo esc_url($title_image);?>" alt="<?php the_title();?>" />
</div><!-- .image -->
<?php endif; // header_image ?>
<h2 class="title"><?php the_title();?></h2>
<?php if ($subtitle):?>
<h3 class="subtitle"><?php echo wp_kses($subtitle,''); ?></h3>
<?php endif;?>
</div><!-- .pad -->
</div><!-- .container -->
</div><!-- .title-area -->
<?php }
}else{ ?>
<div class="content-area">
<div class="container">
<div class="row-fluid"> <!-- added for styling -->
<div id="primary" class="span8"> <!-- added for styling -->
<?php the_content();?>
</div> <!-- added for styling #primary -->
<?php get_sidebar('video-sidebar');?>
</div><!-- .row-fluid -->
</div><!-- .container -->
<div class="clearfix"></div>
</div> <!-- content-area -->
</div><!-- .wi-page -->
<?php } ?>
<?php
endwhile; // have posts
endif; // have posts
if (!empty($pages_included)) :
wp_reset_query();
endif;
// in case onepage disabled but user wants to use onepage template for some usage
if ( disable_onepage() ):
if (have_posts()) :?>
<div class="container">
<?php while(have_posts()): the_post(); ?>
<article <?php post_class('article wi-single');?> id="post-<?php the_ID();?>">
<?php the_content();?>
<?php wp_link_pages( array( 'before' => '<div class="page-links-container"><div class="page-links"><div class="page-links-label">' . __( 'Pages:', 'wi' ) . '</div>', 'after' => '</div></div>', 'pagelink' => '<span>%</span>' ) ); ?>
</article><!-- .article -->
<?php endwhile;?>
</div><!-- .container -->
<?php endif;
endif; // if disable onepage
?>
<?php get_footer();?>
<?php
if ($post->ID == '4048') {
?>
<div <?php post_class('wi-page '.$page_template);?> id="<?php echo esc_attr($post->post_name);?>">
<?php if ( !$hide_title ){ ?>
<!-- TITLE -->
<div class="title-area">
<div class="container">
<div class="pad">
<?php if ( $title_image ) : ?>
<div class="image">
<img src="<?php echo esc_url($title_image);?>" alt="<?php the_title();?>" />
</div><!-- .image -->
<?php endif; // header_image ?>
<h2 class="title"><?php the_title();?></h2>
<?php if ($subtitle):?>
<h3 class="subtitle"><?php echo wp_kses($subtitle,''); ?></h3>
<?php endif;?>
</div><!-- .pad -->
</div><!-- .container -->
</div><!-- .title-area -->
<?php }
}else{ ?>
<div class="content-area">
<div class="container">
<div class="row-fluid"> <!-- added for styling -->
<div id="primary" class="span8"> <!-- added for styling -->
<?php the_content();?>
</div> <!-- added for styling #primary -->
<?php get_sidebar('video-sidebar');?>
</div><!-- .row-fluid -->
</div><!-- .container -->
<div class="clearfix"></div>
</div> <!-- content-area -->
</div><!-- .wi-page -->
<?php } ?>
You forgot to close in line number 3 ?>

Exclude specific taxonomy from query

i have a problem, in first this is my code
$page_id = get_queried_object_id();
$post = get_post($page_id);
$filter_args = array(
'tax_query'=>array(),
'meta_query'=>array()
);
$posts_per_page = 5;
$args = array(
'posts_per_page' => $posts_per_page,
'post_type' => 'property',
'orderby' => 'date'
);
$args = array_merge($args, $filter_args);
$query = new WP_Query( $args );
$items = array();
if(count($query->posts) > 0){
$items = $query->posts;
}
$acf_fields = get_acf_fields(array('general_property_fields', 'property_price'));
foreach($items as $k=>$v){
$items[$k]->acf = get_acf_data($v->ID, $acf_fields);
$items[$k]->pc = sb_get_price($v->ID);
}
The get_acf_fields is a personal function, not worry in this question about that.
And this is my loop for get the values i want.
<?php foreach ($items as $v) {
$status = $v->acf['c_status'];
$status_name = get_term_by('id', $status, 'type');
?>
<?php if ($status_name) { ?>
<div class="sb-sidebar-featured-type">
<?php
$title_status = $status_name->name;
$status = explode(' ', $title_status);
?>
<div class="sidebar-featured-transaction"><?=($status_name) ? $status[0] : '';?></div>
</div>
<?php } ?>
All work fine, but the status give me all values from the taxonomy with name status, for example if i obtain "sold", "discount", "offer", i want to exlude the results with "sold" in the taxonomy but i dont know how make that, thanks :D
Will you please try like this:
<?php if ($status_name != 'sold') { ?>
<div class="sb-sidebar-featured-type">
<?php
$title_status = $status_name->name;
$status = explode(' ', $title_status);
?>
<div class="sidebar-featured-transaction"><?=($status_name) ? $status[0] : '';?></div>
</div>
<?php } ?>
Here is the code working for somelse
<?php foreach ($items as $v) {
$tax_type = $v->acf['c_type'];
$status = $v->acf['c_status'];
$type_name = get_term_by('id', $tax_type, 'type');
$status_name = get_term_by('id', $status, 'type');
if ($status_name) {
$check_status = trim($status_name->slug);
}
if ($check_status != 'sold' && $check_status != 'vendido') {
?>
<div class="sidebar-featured-cont">
<div class="sidebar-featured">
<a class="sidebar-featured-image" href="<?=get_permalink($v->ID)?>">
<img src="<?=$v->acf['c_gallery'][0]['sizes']['property-listing']?>" alt="" />
<?php if ($status_name) { ?>
<div class="sb-sidebar-featured-type">
<?php
$title_status = $status_name->name;
$status = explode(' ', $title_status);
?>
<div class="sidebar-featured-transaction"><?=($status_name) ? $status[0] : '';?></div>
</div>
<?php } ?>
</a>
<div class="sidebar-featured-title"><?=$v->post_title?></div>
<div class="sidebar-featured-price">$ <?=$v->pc?></div>
<div class="clearfix"></div>
</div>
</div>
<?php } ?>
<?php } ?>

Categories