Prevent Post Duplication - wordpress

I am in charge of managing this site F9 Properties which is built in WordPress. On the home page there is a featured properties section. I noticed that if you listed a property with two different "Status" such as "For Sale or For Lease, the property appeared twice in the carousel. Below is the code for listing the featured properties. I can see that it filters out the properties with the Status "Leased". Can anyone help me add a bit of code to list only one property per post regardless of how many different property status it has?
<?php
/* Featured Properties Query Arguments */
$featured_properties_args = array(
'post_type' => 'property',
'posts_per_page' => 100,
'meta_query' => array(
array(
'key' => 'REAL_HOMES_featured',
'value' => 1,
'compare' => '=',
'type' => 'NUMERIC'
)
)
);
$featured_properties_query = new WP_Query( $featured_properties_args );
if ( $featured_properties_query->have_posts() ) :
?>
<section class="featured-properties-carousel clearfix">
<?php
$featured_prop_title = get_option('theme_featured_prop_title');
$featured_prop_text = get_option('theme_featured_prop_text');
if(!empty($featured_prop_title)){
?>
<div class="narrative">
<h3><?php echo $featured_prop_title; ?></h3>
<?php
if(!empty($featured_prop_text)){
?><p><?php echo $featured_prop_text; ?></p><?php
}
?>
</div>
<?php
}
?>
<div class="carousel es-carousel-wrapper">
<div class="es-carousel">
<ul class="clearfix">
<?php
while ( $featured_properties_query->have_posts() ) :
$featured_properties_query->the_post();
?>
<?php
$status_terms = get_the_terms( $post->ID,"property-status" );
if(!empty( $status_terms )){
foreach( $status_terms as $status_term ){
if($status_term->name=="Leased"){}else{
?>
<li>
<figure>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
the_post_thumbnail('property-thumb-image',array(
'alt' => get_the_title($post->ID),
'title' => get_the_title($post->ID)
));
?>
</a>
</figure>
<h4><?php the_title(); ?></h4>
<p><?php framework_excerpt(8); ?> <?php _e('Know More','framework'); ?> </p>
<span class="price"><?php property_price(); ?></span>
</li>
<?
}
}
}
?>
<?php
endwhile;
wp_reset_query();
?>
</ul>
</div>
</div>

I might be misunderstanding your setup, but I wonder why you're looping over the terms.
I think you should instead consider excluding the leased term within the WP_Query() part (hopefully you can share it).
Then your carousel would be simplified to:
<div class="carousel es-carousel-wrapper">
<div class="es-carousel">
<ul class="clearfix">
<?php while ( $featured_properties_query->have_posts() ) : $featured_properties_query->the_post(); ?>
<li><!-- YOUR POST ITEM HERE --></li>
<?php endwhile; ?>
</ul>
</div>
</div>

You can add the post ID to an array every time an iteration occurs, and check the array if the post has already been rendered:
$shown = array(); // new array
while ( $featured_properties_query->have_posts() ) :
$featured_properties_query->the_post();
$status_terms = get_the_terms( $post->ID, 'property-status' );
if( ! empty( $status_terms ) ){
foreach( $status_terms as $status_term ){
if( $status_term->name == "Leased" || in_array( $post->ID, $shown ){
continue; // post has term "Leased" or already rendered, skip
}
$shown[] = $post->ID; // add post ID to array
?>
<!-- HTML here -->
<?php
}
}
endwhile;

Related

How to display posts by category in a static page on WordPress

do you know which code should I use to display posts by category in a static page?
<div>
// This is a static page
// Here I want to display posts from specific category ex. category named hello
</div>
The Complete Implementation for fetching the post from a Specific Category and Display the post in a pages layout if you have more post.
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 10,
'category_name' => ‘your category’,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();?>
<h4><?php the_title(); ?></h4>
<h5><?php the_time('F j, Y'); ?></h5>
<div>
<?php if( has_excerpt() ): the_excerpt(); else: the_content(); endif; ?>
</div>
<?php }?>
//Page navigation
<div id="pagenav" class="navigation clearfix">
<div class="nav-prev alignleft"><?php next_posts_link( '<i class="fa fa-angle-double-left" aria-hidden="true"></i> Older Entries', $the_query->max_num_pages ); ?></div>
<div class="nav-prev alignright"><?php previous_posts_link( 'Newer Entries <i class="fa fa-angle-double-right" aria-hidden="true"></i>' ); ?></div>
</div><!-- #pagenav -->
<?php echo '</div>';
} else {
// no posts found
echo '<h1>No Posts Found</h1>';
}
// Restore original Post Data
wp_reset_postdata();
?>
Use the following:
[display-posts category="fishing,hiking"]
There are lots of other arguments you can give. This link will be useful: https://en.support.wordpress.com/display-posts-shortcode/

ACF + Isotope: Filter Post Object Data

I'm trying to filter data from a custom post-type called "Clients", based on category. What I need to appear is the logo for each particular client.
I've set up a repeater field of Post Objects, so I can change the order that the logos will be displayed.
What I have currently works, however I cannot figure out how to incorporate the Post Object selector, so that what appears is determined by the instances I've added via the Repeater.
Here is the link to the site. Appreciate any answers!
See below for screenshot of my dashboard setup:
<ul id="filters">
<?php
$terms = get_terms("category", array(
'orderby' => 'slug'
)); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php $the_query = new WP_Query(array(
'post_type' => 'clients',
'posts_per_page' => '-1',
'order' => 'ASC'
)); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="isotope-list-container">
<div id="isotope-list" class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> portfolio columns"> <?php // 'portfolio' is used as an identifier (see Setp 5, line 6) ?>
<div class="portfolio-item-container">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</div> <!-- end portfolio item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
I was able to make this considerably simpler by using the Post Types Order plugin to reorder my posts.
https://wordpress.org/plugins/post-types-order/

how to specify custom category (slug) in wordpress theme?

I have purchased a theme whose developer dropped the project and is offering no guidance.
What I want to do is simply show the portfolio category "featured" in the homepage grid. Thought I could change a few things in the php files but nothing is working and I really need a better understanding and solution.
here is a breakdown of the code. I have replaced the portfolio_category with featured and added a taxonomy of 'featured' in the array. the theme calls for me to enter the Category Taxonomy Slug for the Portfolio and replace the default "portfolio_category." Didn't work for me, so I'm not sure where to put what. If there is another file you need me to upload, please let me know.
website: test.muralsdcproject.com
portfolio_page_template.php
Portfolio on homepage file
<?php
/**
* Template Name: Fortfolio - Portfolio
*
* #package Fortfolio
*/
get_header();
/**get_template_part( 'heading' ); */
// get settings
$style = erlbs_meta_data( '_erlbs_select_portfolio_style' ) ? erlbs_meta_data( '_erlbs_select_portfolio_style' ) : erlbs_option_data( 'select_portfolio_style' );
?>
<div class="section active-section no-padding portfolio-section no-gutter <?php echo esc_attr( $style );?>">
<div class="folio-container isotope-container">
<?php
global $wp_query;
$portfolioitems = erlbs_meta_data('_erlbs_select_portfolio_filter_number') ? erlbs_meta_data('_erlbs_select_portfolio_filter_number') : 12 ;
$portfolio_column = $col;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => $portfolioitems,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged
);
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="folio-col isotope-item <?php echo erlbs_custom_taxonomies_terms_slug( 'portfolio_category' );?>">
<?php get_template_part( 'content','portfolio' ); ?>
</div>
<?php
endwhile;
echo paginate_links(
array(
'type' => 'list',
'prev_text' => __('«','fortfolio'),
'next_text' => __('»','fortfolio'),
)
);
?>
</div>
</div>
<?php get_footer(); ?>
taxonomy_portfolio_category
<?php
/**
* Default portfolio category.
*
* #author EverisLabs
* #package Fortfolio
* #version 1.0
*/
get_template_part('archive','portfolio');
?>
content_portfolio
<?php
$portfolio_format = erlbs_meta_data( '_erlbs_radio_portfolio_format' );
?>
<div class="entry">
<div class="entry-media">
<?php if ( $portfolio_format == 'media' ) : ?>
<a href="<?php echo erlbs_meta_data( '_erlbs_portfolio_media_link' ); ?>" class="light-box play" data-lightbox-gallery="gallery1" title="<?php the_title();?>">
<?php the_post_thumbnail( 'portfolio' ); ?>
</a>
<?php elseif ( $portfolio_format == 'gallery' ): ?>
<div class="flexslider">
<ul class="slides">
<?php
$gallery_images = erlbs_meta_data( '_erlbs_portfolio_gallery' );
foreach( $gallery_images as $image => $image_id ) :
$img = wp_get_attachment_image_src( $image, 'post' );
?>
<li><img src="<?php echo esc_url( $img[0] );?>" alt="" class="img-responsive"></li>
<?php endforeach; ?>
</ul>
</div>
<?php elseif ( $portfolio_format == 'lightbox' ): ?>
<a href="<?php echo wp_get_attachment_image_src( get_post_thumbnail_id( get_the_id() ), 'full' ); ?>" class="light-box zoom" data-lightbox-gallery="gallery1" title="<?php the_title();?>">
<?php the_post_thumbnail( 'portfolio' ); ?>
</a>
<?php else: ?>
<a href="<?php the_permalink();?>" class="link" title="">
<?php the_post_thumbnail( 'portfolio' ); ?>
</a>
<?php endif; ?>
</div>
<!--/.entry-media-->
<div class="entry-top">
<h3 class="entry-title"><?php the_title();?></h3>
</div>
<!--/.entry-top-->
</div>
<!--/.entry-->

How to use owl carousel WordPress plugin?

I didn't find enough resource to know about owl carousel WordPress plugin. If you know please explain.
I'd get the image URL and simply loop through the post instances inside a While loop (or THE loop) - essentially, the basic loop for getting post assets.
I'm using the toolset plugin and calling the elements where needed using said loop.
<ul class="attributes">
<?php
$values = array(
// 'author_name' => 'author',
'post_type' => 'attribute',
'orderby' => 'id'
);
$query = new WP_Query( $values );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$name = get_post_meta($post->ID, 'wpcf-value-name', true);
?>
<li>
<div class="values-content">
<div class="title">
<p><?php echo $name; ?></p>
</div>
<div class="text">
<p><?php echo get_post_meta($post->ID, 'wpcf-value-text', true); ?></p>
</div>
<div class="icon">
<?php $icons = get_post_meta($post->ID, 'wpcf-value-icon', true); ?>
<img src="<?php echo $icons; ?>" alt="<?php echo $name; ?>">
</div>
</div>
</li>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
</ul>
If you need any help with this, let me know. If you decide to stick with the plugin, that's fine but I'm not acquainted with it :) I'm calling the 'attributes' class name in the jQuery:
// carousel for about page values
jQuery(".attributes").owlCarousel({
autoWidth: true,
items: 1
});

wp_query by last month + sort posts

I'm using the WordPress plugin 'I recommend this', it allows people to 'like' posts.
The value is stored as a meta key, which we can query to generate a 'most recommended' page.
Plugin -> http://wordpress.org/support/plugin/i-recommend-this
The loop.
<?php
query_posts('&orderby=meta_value&meta_key=_recommended');
if (have_posts()): while (have_posts()) : the_post();
?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
</article>
<?php endwhile; ?>
<?php endif; ?>
This works, it finds the most recommended posts.
Two questions.
How do I limit the posts returned to a date range of say, the last 3 months?
In a similar vein, how could I then have a button for 'Most popular this week' that'd allow users to see a filtered result of posts from the last 7 days?
You could try something like this, this is just a basic example:
$today = getdate();
$threemonths = $today["mon"]-3;
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => $today["year"],
'month' => $threemonths,
'day' => $today["mday"],
),
'before' => array(
'year' => $today["year"],
'month' => $today["mon"],
'day' => $today["mday"],
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
They break it down further in the wordpress documentation:
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
I worked it out. Used a custom filter along with WP_query array.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-180 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$featuredPosts = new WP_Query( array(
'meta_key'=>'_recommended',
'orderby' => 'meta_value_num',
'order' => DESC
) );
remove_filter( 'posts_where', 'filter_where' ); ?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<div class="figure">
<?php the_post_thumbnail('frontthumb'); ?>
</div>
<div class="fig-cover">
<div class="fig-bottom">
<div class="title-container">
<?php if ( get_post_meta($post->ID, 'Price', true) ) { ?>
<div class="price-container">
<?php echo get_post_meta($post->ID, "Price", true); ?>
</div>
<h2 class="price-title"><?php the_title(); ?> </h2>
<?php } else { ?>
<h2><?php the_title(); ?> </h2>
<?php } ?>
</div> <!-- end div title-container -->
</div>
</div>
<div class="reco">
<?php if( function_exists('dot_irecommendthis') ) dot_irecommendthis(); ?>
</div>
<a class="the-post-link" href="<?php the_permalink(); ?> ">
</a>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Now I just need to figure out how to do live filter on-site...

Resources