From what I can see there is no equivalent to wpp_get_views().
Can I use wpp_get_mostpopular() and then loop through the result to display them in a way I want? From my admittedly limited testing, it looks like this will just display them directly, right?
Thanks for your help!
]]>$args = array( 'posts_per_page' => 3 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $poster ) :
echo get_the_ID();
setup_postdata( $poster ); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_date(); the_excerpt(); ?>
<?php endforeach;
wp_reset_postdata();
the_execerpt() shows correctly 3 different values (from the posts), but all other functions show the values from the page where I placed the code.
The page ID is in my case 678, so the_ID() shows 3 times 678,
the_permalink() links to the page 678 and the_title() shows 3 times the title of the page.
Any idea ?
Best regards, Uwe
I have set up a custom post type called Property. The problem is that I just get one Property post appearing on the landing page–the latest post from Cat A1. No posts from Cat A2 or Cat A3 are appearing. If I remove ‘cat’ => 18 under the second array (which calls the Property posts), then I get the latest post from the entire list of Property posts, not from the category specified under the first array (which calls the child categories).
Fairly new to this so any help would be appreciated! My code is below.
Website: https://gmdevco.com/
Code:
<!-- call each child category under cat 18 -->
<?php
$args = array( 'child_of' => 18, 'orderby' => 'date', 'order' => 'DESC', 'number' => 20 );
$categories = get_categories( $args );
foreach( $categories as $post )
setup_postdata( $post ); { ?>
<!-- call one post from current child category -->
<?php
$args = array( 'post_type' => 'property', 'posts_per_page' => 1, 'orderby' => 'date', 'order'=> 'DESC', 'cat' => 18 );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
(Under this code, I have a div that displays the Property info, and that’s all working fine.)
]]>I want to get the content of a custom field inside a walker class for wp_nav_menu, but it seems that the setup_postdata () does not setup the meta values. Does someone know how to solve this?
Thanks
Maestro
https://www.ads-software.com/plugins/custom-content-type-manager/
]]>Tried is_empty or if else statements but having trouble implementing and syntax problems.
<div id="accordion" style="width:95%;">
<?php
$args=array(
'child_of' => 4,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '0'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<h3>' . $category->name . '</h3>
<div>';
global $post;
$args = array( 'posts_per_page' => -1, 'category' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC' );
//alternatively this also works: 'nopaging' => true
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
echo '<p><a href="' . get_permalink($post->ID) . ' " style="color:#7c7c7c; font-size: 1em; text-decoration:none !important; line-height:80%;">' . get_the_title($post->ID) . '</a></p>';
endforeach;
echo '</div>
';
}
?>
</div><!-- accordion -->
]]>What isnt working is any posts2posts connections inside of the setup_postdata($post);
Now before you say “use $post->ID ” for the connection item, I have tried this. Here’s my current setup.
This registers a post 2 post connection and connects one custom post type (band) to another (show).
p2p_register_connection_type( array(
'name' => 'band_to_shows',
'from' => 'band',
'to' => 'show'
) );
I know there are connections made with each show because I see them all in the backend.
The goal of this is to display the connected band on each show post.
Now, for the function. This function uses your current location (found elsewhere) and gets the latitude and longitude, compares that to the latitude and longitude of the postmeta in the database and returns the results if the post is within 240 miles of you.
That function works just fine. It’s the loop-shows.php that isnt working with posts2posts for some reason. (SEE BELOW FOR CODE SNIPPET).
<?php
foreach($nearby_shows as $post): setup_postdata( $post );
?>
<li class="show" data-distance="<?php echo round($distance); ?>">
<div class="showinfo">
<?php
// THE CODE BELOW DOES NOT ECHO OUT ANYTHING
$connected = p2p_type( 'band_to_shows' )->get_connected( $post->ID );
while ( $connected->have_posts() ) : $connected->the_post();
the_title();
endwhile;
}
the_title();
endforeach();
?>
https://www.ads-software.com/extend/plugins/posts-to-posts/
]]><?php
$poststoshow = 2;
if(is_single()) { $currentpost = get_the_ID(); }
$posts = get_posts('numberposts='.$poststoshow.'&category=99'); ?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
static $count1 = 0;
if ($count1 == "2") {
break;
}
else { ?>
<div class="packshot">FIRST ROW POSTS</div>
<?php
//the_content();
$count1++;
}
} ?><hr class="break" />
<?php
unset($posts);
$posts = get_posts('numberposts='.$poststoshow.'&offset='.$count1.'&category=99');
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
static $count2 = 2;
if ($count2 == "4") {
break;
}
else { ?>
<div class="packshot">SECOND ROW POSTS</div>
<?php
//the_content();
$count2++;
}
}
?><hr class="break" />
<?php
unset($posts);
$posts = get_posts('numberposts='.$poststoshow.'&offset='.$count2.'&category=99');
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
static $count3 = 4;
if ($count3 == "6") {
break;
}
else { ?>
<div class="packshot">THIRD ROW POSTS</div>
<?php
//the_content();
$count3++;
}
}
?> <hr class="break" />
With apologies for layout – wasn’t sure how it’d render here.
So on the 4th post (on the second line), it simply isn’t pulled in. Then, no posts appear after that. If I change the third row code to
foreach ($posts as $post) {
setup_postdata($post);
static $count3 = 6;
if ($count3 == "8") {
break;
}
Then posts are pulled in, but two are missing in the middle.
Changing the static $count pulls in different posts but no matter what I do, there always seems to be one missing. Compare this with the same code used to pull in rows of 3, elsewhere on the site:
<?php
$poststoshow = 3;
if(is_single()) { $currentpost = get_the_ID(); }
$posts = get_posts('numberposts='.$poststoshow.'&category=3'); ?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
static $count1 = 0;
if ($count1 == "3") {
break;
}
else { ?>
<div class="packshot">FIRST ROW POSTS</div>
<?php
//the_content();
$count1++;
}
} ?><hr class="break" />
<?php
unset($posts);
$posts = get_posts('numberposts='.$poststoshow.'&offset='.$count1.'&category=3');
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
static $count2 = 3;
if ($count2 == "6") {
break;
}
else { ?>
<div class="packshot">SECOND ROW POSTS</div>
<?php
//the_content();
$count2++;
}
}
?><hr class="break" />
<?php
unset($posts);
$posts = get_posts('numberposts='.$poststoshow.'&offset='.$count2.'&category=3');
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
static $count3 = 6;
if ($count3 == "9") {
break;
}
else { ?>
<div class="packshot">THIRD ROW POSTS</div>
<?php
//the_content();
$count3++;
}
}
?>
This works perfectly.
Any ideas? I must be doing something wrong somewhere along the line…
]]>[Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]
Problem is this: get_posts() does return needed posts properly, and in a foreach loop I prepare each post using setup_postdata(). However, the_permalink() and the_title() always return values from the last post on forum, not from post prepared using setup_postdata().
the_excerpt() does return proper exceprt form current post I want to display. So I get pretty messy result.
I’ve spent substantial time trying to figure out what I am doing wrong, but I cannot tell.
Any help?
]]>I am trying to gather the posts from the database using $wpdb and the closest I have found is this article in the codex:
https://codex.www.ads-software.com/Displaying_Posts_Using_a_Custom_Select_Query
The problem lies in the way it uses setup_postdata($post); to populate template tags and then you are supposed to use the tags such as the_title or the_content in order to use the data. When you use one of those template tags, it automatically echos the data to the screen whereas I want to just hold it in a variable for processing.
How can I get the post data without using template tags?
]]>