Multiple loop issue - pull 1 featured and continue the loop without it - php

I am writing a custom multiple loop to be used on a custom category template page. The loop should put one post that is checked as featured in admin, in a separate div, and continue the loop displaying all posts from the category except the featured.
Similar to the example provided on the codex page except I don't want to create a separate category for the featured post.
I am using Advanced Custom Fields plugin for the check box that sets posts as featured.
I have the following issue with my code: if ($post->ID == $do_not_duplicate) continue; prevents rest of the loop to be executed. The code below just pulls the latest featured post.
Here is my function:
function featured() {
$featured = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'featured',
'value' => '"top"',
'compare' => 'LIKE'
)
),
'posts_per_page' => 1
));
while ( $featured->have_posts() ) : $featured -> the_post();
$do_not_duplicate = $post->ID; ?>
<div id="featured">
//featured post
</div><!-- end #featured -->
<?php
endwhile;
if(have_posts()) : while (have_posts()) : the_post();
if ($post->ID == $do_not_duplicate) continue;
?>
<div class="container">
// normal posts
</div><!-- .charities-container -->
<?php
endwhile;
endif;
}
Your fresh eyes will help a lot!
Thanks!

Looks like your $post variable isn't being assigned with the current post information through the loop. As far as I can tell, you could try either one of these ways:
1) Global $post
The $post variable is within your featured() function scope. Therefore, when you run your loop, it is not being recognized as the global $post variable, which is the one WordPress fills with post information through the loop. Simply declare $post as a global variable at the beginning of your function scope and you should be able to gather post information:
function featured() {
global $post;
// ... all your function code, $post->ID should work now
}
or 2) Use get_the_ID()
You could replace $post->ID for WP's native function get_the_ID(). This is just about the same as the previous solution, as this function will retrieve the ID property from the global $post object automatically. I would take this as the best solution, as you don't have to worry about scope whenever using post functions (get_the_ID(), get_the_title(), etc) as long as the $post object is populated (after the_post() is called).
So you could replace this line:
$do_not_duplicate = $post->ID;
for
$do_not_duplicate = get_the_ID();
and
if ($post->ID == $do_not_duplicate) continue;
for
if (get_the_ID() == $do_not_duplicate) continue;
Try either one of these solutions, I bet both should work for you. Actually, the example you took from codex page works just fine, the problem is that you applied it inside a local function. This way your $post variable is a local (function scope) variable, and not a global one.

Related

looping through pages and updating ACF field data for each inside of a function

Having a bit of an issue and hoping for some help. Basically I am triggering a function on acf/save_post action for a custom post type called 'settings'. So if 'settings' is saved or updated I want to loop through all existing pages and update a ACF field for each page with a specific piece of data. My loop is below. I've been fighting with this thing all weekend but it doesn't seem to update any pages. Any help would be huge! Thanks!
$the_query = new WP_Query( array( 'post_type' => 'page' ) );
while ( $the_query->have_posts() ) : $the_query->the_post();
global $post;
$pageID = $post->ID;
update_field('temp_post_data','test' , $pageID);
endwhile;
You are trying to update field on current page.
Try this
function my_custom_function() {
$pages = get_pages(); // get all pages
foreach($pages as $page):
// error_log($page->ID); // debug if needed
update_field('temp_post_data','test' , $page->ID); //for each page we are updating field
endforeach;
}

Displaying content of Advanced Custom Fields

I’m having a little problem understanding what (my) PHP is doing when I’m trying to display different Custom Posts and Advanced Custom fields on the same page.
I have added different Advanced Custom Fields to a page, and I have custom posts that I am trying to display using the template.
I’m calling my custom fields throughout the template using:
<?php the_field(‘field-name’) ?>
My custom posts are called through loops like this (somewhere around the middle of the template):
<?php
$args = array(
'post_type' => ‘foo’
);
$foo = new WP_Query( $args );
if( $foo->have_posts() ) {
while( $foo->have_posts() ) {
$foo->the_post();
?>
<?php the_content() ?>
<?php
}
}
else {
// SOME MESSAGE
}
?>
The content of the Advanced Custom Fields is showing fine, above those loops. Below the loops it just doesn’t display.
I can’t figure out why the content is not showing up.
I assume it has to do with the while or if statements of the loops. If I remove the loops, the content of any Advanced Custom Field below does display.
When you use WP_Query(), you are changing the default $post variable on the page each time you loop through a post. You need to call wp_reset_postdata() after your loop to reset that $post variable so it corresponds to the current page again. You can call the function after your 'while' loop -
<?php
$args = array(
'post_type' => ‘foo’
);
$foo = new WP_Query( $args );
if( $foo->have_posts() ) {
while( $foo->have_posts() ) { $foo->the_post();
the_content();
} wp_reset_postdata();
}
else {
// SOME MESSAGE
}
?>

How to display most viewed posts in custom post type

I'm using keremiya theme for wordpress. I was trying to display my most viewed post in my custom post type if "most_viewed" option is on. The name of my custom post type is watch. How can i do this with my current code? I am also using a plugin called wp-post views to display the views in my sidebar. Here is my query.
<?php if(get_option('most_viewed') == 'On'): ?>
<div class="sidebar-right">
<h2><?php echo get_option('my_title'); ?></h2>
<div class="fimanaortala">
<?php $tavsayi = get_option('keremiya_tavsiyesayi'); $tavkat = get_option('keremiya_tavsiyekat');?>
<?php query_posts('showposts='.$tavsayi.'&v_orderby=desc&cat='.$tavkat.'') ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="filmana">
<div class="filmsol">
<?php keremiya_resim('80px', '70px', 'izlenen-resim'); ?>
</div>
<div class="filmsag">
<div class="filmsagbaslik">
<?php the_title(); ?>
</div>
<div class="filmsagicerik">
<?php if(function_exists('the_views')) { the_views(); echo " "; } ?>
<p><?php nezaman_yazildi(); ?></p>
</div>
<div class="filmizleme">
<img src="<?php bloginfo('template_directory'); ?>/images/filmizle.png" alt="film izle" height="21" width="61" />
</div>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
Your solution (or attempt, at least), is based on a plugin called WP-PostViews, which I have no knowledge of. So, I can't really help you there. I can, however, help you solve it without this or any other plugin. So here we go:
Wordpress has a little something called metadata. From this very own link:
The Metadata API is a simple and standarized way for retrieving and manipulating metadata of various WordPress object types. Metadata for an object is a represented by a simple key-value pair. Objects may contain multiple metadata entries that share the same key and differ only in their value.
That means you can create metadata for your custom post type that contains how many times it has been seen. To do so, we create a function:
<?php
function set_views($post_ID) {
$key = 'views';
$count = get_post_meta($post_ID, $key, true); //retrieves the count
if($count == ''){ //check if the post has ever been seen
//set count to 0
$count = 0;
//just in case
delete_post_meta($post_ID, $key);
//set number of views to zero
add_post_meta($post_ID, $key, '0');
} else{ //increment number of views
$count++;
update_post_meta($post_ID, $key, $count);
}
}
//keeps the count accurate by removing prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
?>
This will, given the post ID, increment a counter every time a post is viewed. Of course, we have to call this function somewhere in our code so it actually runs. You can do this in two ways: You can either call this function on your single template (which I believe is called single-watch.php), or you can add a simple tracker. I favor the second option, as it keeps your single post loop cleaner. You can achieve such tracking this way:
<?php
function track_custom_post_watch ($post_ID) {
//you can use is_single here, to track all your posts. Here, we're traking custom post 'watch'
if ( !is_singular( 'watch') ) return;
if ( empty ( $post_ID) ) {
//gets the global post
global $post;
//extracts the ID
$post_ID = $post->ID;
}
//calls our previously defined methos
set_views($post_ID);
}
//adds the tracker to wp_head.
add_action( 'wp_head', 'track_custom_post_watch');
?>
There you go. Now, WordPress checks if the user is visiting a page corresponding to a watch single post. If they are, it increments the counter.
The only thing left now is to query for the post with the highest number of views. This is easily achievable using WP_Query. When you create your loop, do something like this:
<?php
$query = new WP_Query( array(
'post_type' => 'watch', //your post type
'posts_per_page' => 1,
'meta_key' => 'views', //the metakey previously defined
'orderby' => 'meta_value_num',
'order' => 'DESC'
)
);
while ($query->have_posts()) {
$query->the_post();
//whatever code you want
}
?>
I kept my answer to PHP, so you can adapt to your mark-up needs. I also assumed your post-type is indeed called watch. I hope all of this helps you. If you want to query your posts in a slightly different way, I suggest you read the WP_Query docs. Cheers.

Wordpress, global post changed after query

I am developing a plugin. In this plugin, I've created a custom post type 'toto'.
In the admin page, I edit a custom post element with the following post id '82'.
In this page, I launch a query to retrieve elements with another post_type like that :
$featured_args = array(
'post_type' => 'other_type',
'post_status' => 'publish'
);
// The Featured Posts query.
$featured = new WP_Query( $featured_args );
// Proceed only if published posts with thumbnails exist
if ( $featured->have_posts() ) {
while ( $featured->have_posts() ) {
$featured->the_post();
if ( has_post_thumbnail( $featured->post->ID ) ) {
/// do stuff here
}
}
// Reset the post data
wp_reset_postdata();
}
Doing that the global $post changed. It is not the post with the id 82 anymore but the latest post element from the query.
I thought that the wp_reset_postdata() function will allow me to retrieve my current $post. I also tried with wp_reset_query() without changes.
Am I missing something?
Any help would be appreciated.
wp_reset_postdata() resets the main loop. If you want to access $post directly try
global $post;
$backup_post = $post;
//do another loop
wp_reset_postdata();
$post = $backup_post;

WP custom post type UI

I use WP plugin "Custom Post Type UI" and a template of my own to add some content to my wp page.
<div id="AboutMain">
<?php
/* the original page contents */
while (have_posts()) {
the_post();
get_template_part('content', 'page');
/* New set of data fetched from my custom post types */
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'styrelse'
));
if($posts)
{
foreach($posts as $post)
{
$namn=get_the_title($post->ID);
$desc=get_the_content($post->ID);
echo "
<div class='styrelsen art-postcontent'>
<h5 class='art-postcontent'>$namn</h5>
<p>$desc</p>
</div>
";
}
}
Now I get a really strange behavior - since I have set the $posts with a custom query, in the for-each loop it should not contain the basic page contents anymore but my custom post type content right?
But the get_the_content($post->ID); fetches me the original contents of the page, and repeats it over and over while the get_the_title($post->ID); retrievs for me the correct title.
******** solved *************'
OK, here's how I did it.
first I realized that using that global variable name might not be the best idéa so I changed it to $board_posts.
Then I dumped that variable to find out how to access the title and body 'manually' and it turns out it was easier than I first thought.
foreach($styrelse_posts as $post)
{
$namn = $post->post_title;
$desc = $post->post_content;
trial and error beats most issues =)
Using $post as a variable can cause some weird behavior because of how Wordpress uses that as a global variable. I'd suggest trying either calling setup_postdata($post) before get_the_title($post->ID), or maybe changing the foreach to foreach($posts as $p) and using $p->ID as the argument.

Categories