code stopped working after wordpress update 4.8.2 - php

I tried with the code provided but it stil doesn't work...
and it started giving problems only after updating to wordpress 4.8.2
I copy also the code with the following parte where I close the foreach and the if ...
With Wordpress 4.7.6 works perfectly...
Any Idea?
Thanks a lot for the help!
<?php
$cat_array = array();
$args = array(
'post_type' => 'post',
'posts_per_page' => 9,
'ignore_sticky_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts())
{
while ($my_query->have_posts()):
$my_query->the_post();
$cat_args = array(
'orderby' => 'none'
);
$cats = wp_get_post_terms($my_query->post->ID, 'category', $cat_args);
foreach($cats as $cat)
{
$cat_array[$cat->term_id] = $cat->term_id;
}
endwhile;
wp_reset_postdata();
}
if ($cat_array)
{
foreach($cat_array as $cat)
{
$category = get_term_by('ID', $cat, 'category');
$slug = get_term_link($category, 'category');
$id = $category->term_id;
$valore = "category_" . $id;
$colore = get_field('colore_categoria', $valore);
$immagine = get_field('immagine_ispirazione', $valore);
$testo_box = get_field('testo_box', $valore);
?>
<div class="colonna clearfix">
<a href="<?php echo $slug;?>">
<div class="box">
<img src="<?php echo $immagine?>" alt="italia">
<div class="overlay">
<p><?php echo $testo_box;?></p>
</div>
<div class="titolobox" style="background-color:<?php echo $colore;?>">
<h2><?php echo $category->name;?></h2>
</div>
</a>
</div>
</div>
<?php
}
}
wp_reset_query();
?>

Use 'ignore_sticky_posts' instead of 'caller_get_posts'.
You can refer to this page to update your query

I don't know your requirement but I think you didn't close the the foreach loop and if condition, and also I think that you need advance custom field value of category assigned to the post for that you should use
wp_get_post_categories( int $post_id, array $args = array() )
in your while loop. hope this help.
and you should use "ignore_sticky_posts" rather than "caller_get_posts"

use bellow code.
<?php
$cat_array = array();
$args = array(
'post_type' => 'post',
'posts_per_page' => 9,
'ignore_sticky_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts())
{
while ($my_query->have_posts()):
$my_query->the_post();
$cat_args = array(
'orderby' => 'none'
);
$cats = wp_get_post_terms($my_query->post->ID, 'category', $cat_args);
foreach($cats as $cat)
{
$cat_array[$cat->term_id] = $cat->term_id;
}
endwhile;
wp_reset_postdata();
}
if ($cat_array)
{
foreach($cat_array as $cat)
{
$category = get_term_by('ID', $cat, 'category');
$slug = get_term_link($category, 'category');
$id = $category->term_id;
$valore = "category_" . $id;
$colore = get_field('colore_categoria', $valore);
$immagine = get_field('immagine_ispirazione', $valore);
$testo_box = get_field('testo_box', $valore);
}
}
?>
enjoy

Related

Wordpress how to insert a div tag in the middle of the loop of related posts

How to insert a div after the third post of this wordpress loop (code below) ??
I searched the site but I couldn't find an explanation of how to insert it in a code like mine.
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'orderby' => 'rand',
'showposts'=>6, //
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<div id="loopindex">';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<div class="post_mobile" id="post-<?php the_ID(); ?>">
CONTENT
</div>
<?php
}
echo '</div>';
}
}
?>
All you need is a counter! Before your while loop starts, initialize a counter variable.
Then at the beginning of your while loop check the counter value, and at the end of while loop add 1 to it. Like so:
$your_query = new wp_query($args);
$counter = 1;
while ($your_query->have_posts()) {
// first check the value of your counter variable
if (3 == $counter) {
// output your div/html here
}
// do your stuff in the loop if you need to!
// right at the end of your while loop add 1 to your counter
$counter++;
}
Use WordPress current_post to check the post index in while loop.
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'orderby' => 'rand',
'showposts'=>6, //
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<div id="loopindex">';
while ($my_query->have_posts()) {
$my_query->the_post();
// Get the current post index
$current_post = $my_query->current_post;
//Check if post is 4th post
if($current_post == 3){?>
<div>This content is displayed only after third post.</div>
<?php
}
?>
<div class="post_mobile" id="post-<?php the_ID(); ?>">
CONTENT
</div>
<?php
}
echo '</div>';
}
}
?>

How can I display projects that only use a tag of my choosing. Currently all projects are being fetched

I'm trying to modify a projects.php file which uses some loops and if statements to fetch projects based on there tags. I need to modify thee below code to only fetch projects with the tag new. Currently all the projects are being fetched and displayed.
Any help would be great. See code attached.
Thanks
<div id="projects" class="clearfix">
<?php $page_skills = get_post_meta($post->ID, "_ttrust_page_skills", true); ?>
<?php $skill_slugs = ""; $skills = explode(",", $page_skills); ?>
<?php if (sizeof($skills) >= 1) : // if there is more than one skill, show the filter nav?>
<?php $skill = $skills[0]; ?>
<?php $s = get_term_by( 'name', trim(htmlentities($skill)), 'skill'); ?>
<?php if($s) { $skill_slugs = $s->slug; } ?><?php endif;
$temp_post = $post;
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 200,
'post_type' => 'project',
'skill' => $skill_slugs
);
$projects = new WP_Query( $args );
endif; ?>
<div class="wrap">
<div class="thumbs masonry">
<?php while ($projects->have_posts()) : $projects->the_post(); ?>
<?php
global $p;
$p = "";
$skills = get_the_terms( $post->ID, 'skill');
if ($skills) {
foreach ($skills as $skill) {
$p .= $skill->slug . " ";
}
}
?>
<?php get_template_part( 'part-project-thumb'); ?>
<?php endwhile; ?>
<?php $post = $temp_post; ?>
</div>
</div>
you should just need to add a field in your $args array :
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 200,
'post_type' => 'project',
'skill' => $skill_slugs,
'tag' => 'new' // add this for the tag
);
$projects = new WP_Query( $args );
Let me know if it works :D

Display post list by custom post type and category

Hi i need to display list of post by post type and category, i have code like this, but it isint working properly:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename&cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?>
This code displaying post from "posttypename", but it displays all post from that custom post type, but i need to displaying post from only "categoryname"
The whole code looks like this:
<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename%cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?> <?php if($i == 1) : ?> <div class="">content of the post</div> <?php endif; ?> <?php $i++; endwhile; ?>
Use WP_Query like this :
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname',
// 'cat' => cat ID here
));
if( $catquery->have_posts() ){
while($catquery->have_posts()){
$catquery->the_post();
// your stuff
}
wp_reset_postdata();
}
Try below code
<?php
$catquery = new WP_Query(array(
'post_type' => 'posttypename',
'posts_per_page' => -1,
'category_name' => 'categoryname'
));
if( $catquery->have_posts() ){
$i = 1;
while($catquery->have_posts()){
if($i == 1){ ?>
<div class="">content of the post</div>
<?php } ?>
$i++;
}
wp_reset_postdata();
}
?>

Randomise X most recent wordpress posts

I've got the following code which will display 4 random posts from the defined category. However, that's a little too random, I need it to display the 4 most recent, and then randomise them in particular each time the page is refreshed.
Otherwise I just end up getting articles which could date back over 2 years ago appear on the homepage.
<div class="article-block-v1">
<?php
//get terms (category ids 11,2,33,34), then display one post in each term
$taxonomy = 'category';// e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'include' => '1459',
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4,
'orderby' => 'rand',
);
$my_query = null;
$my_query = new WP_Query($args);
$i = 1;
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php if ($i == 1): ?>
<div class="article-block-v1">
<div class="category-label-large category-news-analysis">
<p><?php echo $term->name; ?></p>
</div>
<div class="view2 third-effect">
<?php the_post_thumbnail(); ?>
<div class="mask">
</i>
</div>
</div>
<ul class="homepagewhitebg">
<li><h5><?php the_title(); ?></h5></li>
</ul>
</div>
<?php else: ?>
<li><p><?php the_title(); ?></p></li>
<?php endif; ?>
<?php
$i++;
endwhile;
echo '</ul>';
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Demo 1 (4 Most Recent) - Regular without random orderby
Article #1001
Article #1002
Article #1003
Article #1004
Demo 2 (4 Most Recent - But with order by random on those 4)
Article #1003
Article #1001
Article #1004
Article #1002
UPDATE
I'm also trying this now, however it's not taking into account the category I've defined, nor is it randomising across the 4 displayed:
<div class="article-block-v1">
<?php
$number = "4";
$posts = "SELECT * from $wpdb->posts WHERE post_type='post' ORDER BY post_date DESC LIMIT $number";
$postX = array();
$postresult = mysql_query($posts) or die(mysql_error());
while ($row = mysql_fetch_array($postresult)) {
$postX[] = $row[0];
}
$ids = shuffle($postX);
$ids = implode(',', $postX);
echo $ids;
?>
<?php
$args = array(
'posts_per_page'=> $number,
'post__in' => explode(',', $ids),
'include' => '1451',
'orderby' => 'rand'
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
You can use shuffle(). You're going to want to get the 4 most recent posts (in order) before shuffling them. Quite simply, you could do something like the following:
// Get the 4 most recent posts
$args = array( 'numberposts' => 4 );
$recent_posts = wp_get_recent_posts( $args );
// Shuffle them
shuffle($recent_posts)
foreach( $recent_posts as $recent ){
// Do something with the $recent post
}
You may need to pass additional $args to the function, for your specific constraints.

Word "Array" is Appearing on Blog Posts Page

I'm getting the word "Array" coming up on my blog POSTS page when I use the below code. I have a feeling that it relates to this line:
I am using a similar template for my PORTFOLIO and NEWS pages where the wording is slightly different on this line ("showportcat" and "shownewscat" instead of "get_the_category") and it should be displaying the categories but instead, the word 'array' is in it's place.
I've tried "showpostcat" but that didnt' work so I wonder if I need to be re-wording it? Or maybe the problem is on another part of the code which I've included below?
<div class="greyblock">
<h4 class="nomar"><?php echo the_title(); ?></h4>
<div class="sep"></div>
<?php echo get_the_category(get_the_ID()); ?>
<div class="clear"></div>
<ul class="blogpost_list columns2">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => get_option("posts_per_page"),
);
if (isset($_GET['slug'])) {
$args['tax_query']=array(
array(
'taxonomy' => 'postcat',
'field' => 'slug',
'terms' => $_GET['slug']
)
);
}
$wp_query->query($args);
?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();
#We have:
#get_permalink() - Full url to post;
#get_the_title() - Post title;
#get_the_content() - Post text;
#get_post_time('U', true) - unix timestamp
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo "
<li class='pc'>
<img alt='".get_the_title()."' src='".TIMTHUMBURL."?w=400&h=400&src=".$featured_image[0]."'>
<h4>".get_the_title()."</h4>";
$terms = get_the_terms($post->ID, 'postcat');
if ( $terms && ! is_wp_error( $terms ) ) {
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
}
echo "
<p>".get_the_excerpt()."</p>
<a href='".get_permalink()."' class='read'>Read more</a>
<br class='clear' />
</li>
";
endwhile; ?>
</ul>
</div>
<?php get_pagination() ?>
<?php $wp_query = null; $wp_query = $temp; ?>
How about:
$categories = get_the_category(get_the_ID());
foreach ($categories as $category) {
echo $category;
}
The problem is still there. I'm guessing it relates to the way I am wording one of the below two lines as it is 'showportcat' for portfolios and 'shownewscat' for news but 'showpostcat' doesn't work for posts so I just can't figure it out:
<?php echo get_the_category(get_the_ID()); ?>
**OR**
'taxonomy' => 'postcat',

Categories