Exclude duplicated posts from wordpress using index.php with tempates part - php

I'm trying to avoid duplicates posts in one file - index.php where i do collection of the loops for categories. But no result. Please - help...
<div class="container-fluid newgray">
<div class="container asia travel-news proppad">
<?php get_template_part( 'inc/slider-news' );?>
</div>
</div>
<div class="container-fluid best">
<div class="container travel-news proppad">
<?php get_template_part( 'inc/best' );?>
</div>
</div>
<div class="container-fluid newback over">
<div class="container last travel-news proppad">
<?php get_template_part( 'inc/latest' );?>
</div>
</div>
Trying use this code:
<?php
$do_not_duplicate = array(); // set befor loop variable as array
// 1. Loop
query_posts('ca=1,2,3&showposts=5');
while ( have_posts() ) : the_post();
$do_not_duplicate[] = $post->ID; // remember ID's in loop
// display post ...
the_title();
endwhile;
?>
<?php
// 2. Loop
query_posts( 'cat=4,5,6&showposts=15' );
while (have_posts()) : the_post();
if ( !in_array( $post->ID, $do_not_duplicate ) ) { // check IDs
// display posts ...
the_title();
}
endwhile;
?>
But it works only when all loops in one php file…

This is How I wold do, don't waste your time repeating same code, it will be a hell if you need change something.
function post_by_cat_ecluding($cats, $per_page, $exclude = array()) {
$html = '';
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => $cats,
'posts_per_page' => $per_page,
'post__not_in' => $exclude,
'offset' => 0
);
$loop = new WP_Query($args);
foreach ($loop->posts as $p) {
$exclude[] = $p->ID;
$html .= '<article><h2>'.$p->post_title.'</h2></article>';
}
return array('html' => $html, 'exclude' => $exclude);
}
$first_block = post_by_cat_ecluding(array(1,2), 5);
echo $first_block['html'];
$second_block = post_by_cat_ecluding(array(4,5), 15, $first_block['exclude']);
echo $second_block['html'];

Related

the_field - ACF not working in 'while' loop

Main string of code (that doesn't work):
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3,
'type_of_site' => 'landing_page_type',
);
$webTariffs = new WP_Query($args);
while ($webTariffs->have_posts()) {
$webTariffs->the_post();?>
<div class="pricing-item">
<div class="pricing-item-header">
<h3><?php the_title() ?></h3>
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
</div>
</div>
<?php } ?>
The ways i tried to solve this problem:
put insted of the_field('tariff_price_kn') - echo('hi') - the code worked
add $post_id -> the_field('tariff_price_kn', $post_id)
$currencyKune = get_field('tariff_price_kn'); And then echo $currencyKune
P.S 3) i don't know exactly where should i put $currencyKune = get_field('tariff_price_kn'), so i put it before while at first time - doesn't work and at the second time i put it after $webTariffs->the_post();
Try this :
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-6">
<h2 class="the-title"><?php the_field('tariff_price_kn', $post->ID); ?> + <?php the_title() ;?> </h2>
</div>
<?php endwhile; else: ?> Nothing here <?php endif; ?>
<?php wp_reset_query(); ?>

A WordPress loop that displays the latest post from just 4 of your categories

I'm a WP beginner and I have written a loop that is displaying the latest post from each of my categories, but I need to refine the loop so it only displays the latest post from 4 different categories. Can anyone help me restrict the number of categories im looping through? it would be much appreciated.
<div class="latest-updates-container container">
<div class="row">
<div class="col-lg-2">
<div class="latest-update-text">
LATEST UPDATES
</div>
</div>
</div>
<div class="latest-updates-outter-wrapper"></div>
<!--/.container-->
<div class="">
<?php
$do_not_duplicate = array();
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => -2,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) {
$query->the_post();
$do_not_duplicate[] = $post->ID;
if($i % 5 == 0) { ?>
<div class="row latest-post">
<?php
}
?>
<div class="col-lg-3">
<div class="my-inner">
<h5 id="post-<?php the_ID(); ?>" class="blog-heading">
<a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a>
</h5>
<div class="time-read-now"><?php echo reading_time(); ?> ·
read now</h4>
</div>
</div>
</div>
<?php $i++;
if($i != 0 && $i % 5 == 0) { ?>
</div>
<!--/.row-->
<div class="clearfix"></div>
<?php
} ?>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div>
</div>
</div>
</div>
Its displaying the latest post from all categories, I need to just display 4 categories.
The cat parameter allows you to filter posts by category ID. Your code is almost correct, all you need to do now is pass the category ID to the WP_Query class:
<?php
$do_not_duplicate = array();
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => $category->term_id, // Get posts from this category ID
'category__not_in' => array(4, 2), // Exclude posts from categories 4 and 2
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
);
$query = new WP_Query( $args );
// Rest of your code here...

Wordpress Loop inside taxonomy php file CPT Filter By Taxonomy

I have this custom post type loop inside taxonomy.php file on wordpress working fine.
<div class="container">
<?php
$i = 1;
//added before to ensure it gets opened
echo '<div class="row">';
$wp_query = new WP_Query(array('post_type' => 'publica', 'posts_per_page' => 6));
if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();
// post stuff...
?>
<div class="col s12 m4">
<div class="notboxes">
<?php the_post_thumbnail('medium', array('class' => 'responsive-img')); ?>
<?php get_the_term_list($id, $taxonomy, $before, $sep, $after) ?>
<span class="litletimebox"><?php the_time('H:i'); ?> | <?php echo get_the_term_list($post->ID, 'comision-publicaciones') ?></span>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
Leer Más
</div>
</div>
<?php
// if multiple of 3 close div and open a new div
if ($i % 3 == 0)
{
echo '<div style="clear:both"></div></div><div class="row">';
}
$i++;
endwhile;
endif;
//make sure open div is closed
echo '</div>';
?>
</div>
I bring the number of the taxonomy by using this
$queried_object = get_queried_object();
$ter_id = $queried_object->term_id;
But when I put the cat = $ter_id to filter the taxonomy
$wp_query = new WP_Query( array( 'post_type' => 'publica', 'posts_per_page' => 6, 'cat' => $ter_id));
It wont load on the page.
So I've been searching on the web and I found this loop that pulls all taxonomys with its posts.
<?php
$custom_terms = get_terms('comision-publicaciones');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'publica',
'tax_query' => array(
array(
'taxonomy' => 'comision-publicaciones',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post(); ?>
<?php echo get_the_title(); ?>
<?php endwhile;
}
}
?>
This works perfect in my need, but just need the posts from the taxonomy page im on, for example http://comisionpais.com/comision-publicaciones/salud/
Only load the post that have 'salud' marked.
This image shows all the loop
http://imgur.com/a/hSgmX
Why I pasted first code and last code, if it can help in something.

is there a way to display posts based on priority in the loop in wordpress?

I want to display post based on priorities and once they are displayed the priority should be lowered so that when next time page is reloaded or refreshed other posts are on the top and those that were already displayed should be at the bottom.
You can order the post in random order. It will not display posts in same order after refreshing the page. Try this below code:
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('orderby', 'rand');
}}
created a meta field 'priority_priority'
<div id="games-container" class="col-md-12">
<?php
$counter = 0;
$args = array( 'post_type' => 'games', 'posts_per_page' => 10,
'orderby' => 'meta_value_num',
'order' => 'DES',
'meta_query' => array(
array(
'key' => 'priority_priority'
),
), );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
if($counter < 2){
?>
<a href=<?php echo get_post_permalink()?>>
<div id="mypost" class="col-md-5" style="min-height:400px;">
<?php the_post_thumbnail('large');
?>
</div>
</a>
<div class="col-md-1"></div>
<?php
$counter++;
}
else {?>
<a href=<?php echo get_post_permalink()?>>
<div id="mypost" class="col-md-3">
<!-- post display -->
<?php the_post_thumbnail('medium'); ?>
</div>
</a>
<div class="col-md-1"></div>
<?php
}
endwhile;
wp_reset_postdata();
?>
</div>
then used the following code in post template page
<?php
$post_id = get_the_ID();
$priority = get_post_meta($post_id,'priority_priority',true);
if($priority>1)
{
if(!isset($_COOKIE['visitor'.$post_id]))
{
$bool=setcookie('visitor'.$post_id,$post_id,DAY_IN_SECONDS,'/');
if(count($_COOKIE) > 0)
{
$priority = $priority-1;
update_post_meta($post_id,'priority_priority',$priority);
}
}
}
get_header();
?>

different loops to show different post in same category

I have got some problem in showing post.
what i have to do is i want to show post from a category but the condition is first post i want to show in different div in a loop.
Then second and third post of the same category i want show in another div in different loop and rest of the post of the same category in another div and different loop.
i.e.
<div class="post1"> 1st post in the selected category </div>
<div class="post23"> 2nd and 3rd posts in selected category </div>
<div class="post--"> rest all posts in selected category </div>
here is my code
<?php $i = 0;
$args = array( 'offset'=> 0, 'category' => 19, 'numberposts' =>2000, 'order' => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
if ($i ==1 ) {
?>
<li>
<div class="cover_post">
<div class="image_bl"><?php the_post_thumbnail(array(320,245)); ?></div>
<div class="content_hover">
<h6>By : <?php the_author(); ?></h6>
<h3 class="title"><?php echo get_the_title(); ?></h3>
<p> <?php //the_excerpt(); ?> <?php echo substr(get_the_excerpt(), 0,70); ?> ...</p>
<p>Read More
SHARE <span class="facebook_share"> <img src="<?php echo get_template_directory_uri(); ?>/images/facebook_sh.png" alt="" /></span> <span class="twitter_share"> <img src="<?php echo get_template_directory_uri(); ?>/images/twit_sh.png" alt="" /></span>
</p>
</div>
<div class="content_hover content_hover1" style=" visibility:visible">
<h6>By : <?php the_author(); ?></h6>
<h3 class="title"><?php echo get_the_title(); ?></h3>
</div>
<div class="clear"></div>
</div>
<div class="icon_post">
</div>
</li>
<?php $i++; }
endforeach;
wp_reset_postdata();
?>
Can somebody help me?
Thanks in advanced
Just add a condition to the start of the loop. Something like:
switch ($i) {
case 0:
echo '<div class="post1">';
break;
case 1:
case 2:
echo '<div class="post23">';
break;
default:
echo '<div class="post">';
break;
}
Then close the </div> at the end of the loop.
<?php
if($i == 0)
{
?>
<div> Div for first post</div>
<?php
}elseif($i == 1 || $i == 2)
{
?>
<div> Div for second and third posts</div>
<?php
}
else
{
?>
<div> Div for rest posts</div>
<?php
}
?>
Solved:
I have used diffrent Offset value for this
<?php
$args = array( '**offset**'=> 0, 'category' => 19, 'numberposts' =>1, 'order' => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<?php
$args = array( '**offset**'=> 1, 'category' => 19, 'numberposts' =>2, 'order' => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
{
?>
<?php
$args = array( '**offset**'=> 3, 'category' => 19, 'numberposts' =>5, 'order' => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
{
?>

Categories