I have this featured listing and the code needs to count how many active featured listings the user has. I can't make the while loop work here in Wordpress. Here's code I used:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=post&posts_per_page=-1&author='.$user_ID);
$FeaturedAdsCount = 0;
while ($wp_query->have_posts()) : $wp_query->the_post();
$featured_post = "0";
$post_price_plan_activation_date = get_post_meta($post->ID, 'post_price_plan_activation_date', true);
$post_price_plan_expiration_date = get_post_meta($post->ID, 'post_price_plan_expiration_date', true);
$todayDate = strtotime(date('d/m/Y H:i:s'));
$expireDate = strtotime($post_price_plan_expiration_date);
if(!empty($post_price_plan_activation_date)) {
if(($todayDate < $expireDate) or empty($post_price_plan_expiration_date)) {
$featured_post = "1";
}
}
if($featured_post == "1") { $FeaturedAdsCount++; }
endwhile;
$wp_query = null; $wp_query = $temp;
echo $FeaturedAdsCount;
The output of $FeaturedAdsCount is always zero no matter if the user has active featured listing. Am I missing something here? Sorry I am still learning in PHP, self-taught actually. :) Thank you in advance!
PS: This is the postmeta table:
Okay! After hours of reading and googling, it says that when calling get_post_meta inside a loop, always use get_the_id() instead of $wp_query->post->ID or $post->ID. So the final get_post_meta must be like this to get a result inside a loop:
$post_price_plan_activation_date = get_post_meta(get_the_id(), 'post_price_plan_activation_date', true);
$post_price_plan_expiration_date = get_post_meta(get_the_id(), 'post_price_plan_expiration_date', true);
Thank you xphan for all the advice! :)
Related
I am creating a Playlist using LearnDash Plugin,
Below is my code, This code works good but I can't get a video of a particular Lesson
Is there anyone who knows how to fix this?
add_shortcode( 'playlist', 'playlist_for_LMS' );
function playlist_for_LMS() {
global $post;
$user_id = get_current_user_id();
$courses = learndash_user_get_enrolled_courses( $user_id, array(), true );
$lessons = learndash_get_course_lessons_list( $courses[0], $user_id, array(), true );
foreach ($lessons as $lesson){
$postID = $lesson['post']->lesson_video_url;
$postImage = get_the_post_thumbnail($lesson['post']->ID);
$postTitle = $lesson['post']->post_title;
$postLink = get_post_permalink($lesson['post']->ID);
echo ''.$postTitle.' '.$postID.'<br>';
}
}
You use the below code and whats are the your output and go through your out put of fetched data. And also you can get errors if you have.
echo '<pre>';
print_r($lessons);
<?php
$counter=1;
$counter_new=0;
$args = array('posts_per_page' =>-1,'orderby' => 'post_date','order' =>'DESC','post_type' => 'interview','post_status' => 'publish',
'suppress_filters' => true );query_posts( $args );while (have_posts($args)) : the_post();
if($counter < 8)
{
$counter++;
}
else
{
$counter_new++;
$counter=1;
}
endwhile;
?>
I saw someone else code to find the number of post, as record increase it is not efficient. What is the right way to do? It looks stupid now.
In case you use WPML wp_count_post() will not show correct count of posts for given language. Use this instead:
$posts = get_posts('post_type=yourcustomposttype&suppress_filters=0&posts_per_page=-1');
$count = count($posts);
echo $count;
Take a look at the wp_count_posts() function.
For your example:
$count_posts = wp_count_posts('interview');
$published_posts = $count_posts->publish;
$published_posts will return the number of published posts in your 'interview' custom post type.
One of the other answers won't work if you have WMPL and translations. The other one will work, but will be very inefficient.
With WPML, try this instead:
function my_count_posts( string $post_type = 'post', string $language_code = '', string $post_status = 'publish' ): int {
global $wpdb;
$default_language_code = apply_filters( 'wpml_default_language', null );
$language_code = $language_code !== '' ? $language_code : $default_language_code;
$translation_param = $default_language_code == $language_code ? "IS NULL" : "= '{$default_language_code}'";
$query = <<<SQL
SELECT COUNT( {$wpdb->prefix}posts.ID )
FROM {$wpdb->prefix}posts
LEFT JOIN {$wpdb->prefix}icl_translations ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}icl_translations.element_id
WHERE {$wpdb->prefix}icl_translations.language_code = '{$language_code}'
AND {$wpdb->prefix}icl_translations.source_language_code $translation_param
AND {$wpdb->prefix}icl_translations.element_type = 'post_{$post_type}'
AND {$wpdb->prefix}posts.post_status = '$post_status'
SQL;
return $wpdb->get_var( $query );
}
You'd use it like this:
// counts published "posts" in the default language
$count = my_count_posts();
// counts posts of `post_type=custom_post_type` in the current language
$count = my_count_posts('custom_post_type', apply_filters( 'wpml_current_language', '' ));
I have the following code, which basically pulls in a list of terms for the taxonomy "categories". It then pulls in every post for that term.
$terms = get_terms('categories');
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'categories','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h3>";
if ($article_count) {
echo "<ul>";
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
echo "</ul>";
}
}
My question is how would i go about limiting the query to only pull in 1 post from each term?
Any help would be greatly appreciated, Cheers Dan
You can use post_count in $wpq array
eg:- $wpq = array ('taxonomy'=>'categories','term'=>$term->slug,'post_count' => 1);
More about WP_Query http://codex.wordpress.org/Class_Reference/WP_Query
Here is what i got working:
i simply changed the following:
$wpq = array ('taxonomy'=>'categories','term'=>$term->slug,);
to:
$wpq = array ('taxonomy'=>'categories', 'showposts' => 1, 'term'=>$term->slug,);
I need to extract the value of "page" i.e 5 from this url - http://snypher.local/photos/page/5
What should I do to extract it in Wordpress? I am not able to get it from the $_GET super global.
use get_query_var example $page = get_query_var('paged'); in your case it's 5
its work fine i've tested on my current WP (version 3.5.1)
$current_page = max( 1, get_query_var('paged') );
$total_pages = $wp_query->max_num_pages;
echo 'Page '.$current_page.' of '.$total_pages;
Result = Page 3 of 51
function get_url_var($name)
{
$strURL = $_SERVER['REQUEST_URI'];
$arrVals = explode("/",$strURL);
$found = 0;
foreach ($arrVals as $index => $value)
{
if($value == $name) $found = $index;
}
$place = $found + 1;
return $arrVals[$place];
}
$page = get_url_var('page');
I have used this function to get the value of the variable page from the url.
Found a nice solution and I'd like to share it here for I was looking for exactly the same thing!
http://wordpress.org/support/topic/get-current-page-number-for-paginated-posts
So it's like:
<?php echo '(Page '.$page.' of '.$numpages.')'; ?>
Im currently trying to adjust a Content SlideShow Plugin for Wordpress in order to make it compatible with WPML (Multilingual-Plugin). To achieve this, I simply need to fetch the posts from a specific category, put them into an array and return that array. WP_Query gives me a hard time doing this, as it seems like it's fetching the latest post infinite times in the loop. I'm not experienced in writing Wordpress Plugins, so I would be thankful for any hint you can give me.
This is the code of the plugins class method I'm trying to adjust.
function get_valid_posts(){
$validPosts = array();
$this_post = array();
$id_pot = array();
$my_query = new WP_Query('cat=15&showposts=10');
if($my_query->have_posts()) {
while ($my_query->have_posts()) :
$post = $my_query->post;
if(!in_array($post->ID, $id_pot)){
$this_post['id'] = $post->ID;
$this_post['post_content'] = $post->post_content;
$this_post['post_title'] = $post->post_title;
$this_post['guid'] = $post->guid;
array_push($id_pot, $post->ID);
array_push($validPosts, $this_post);
}
endwhile;
}
return $validPosts;
}
Note that I've added the $id_pot array in order to filter duplicate entries, but this shouldn't be necessary if the query / loop would work.
Thanks in advance!
I've managed to solve the problem:
function get_valid_posts(){
$validPosts = array();
$this_post = array();
$id_pot = array();
$i = 0;
$my_query = new WP_Query('category_name=gallery-post&showposts=10');
if($my_query->have_posts()) {
while($i < $my_query->post_count) :
$post = $my_query->posts;
if(!in_array($post[$i]->ID, $id_pot)){
$this_post['id'] = $post[$i]->ID;
$this_post['post_content'] = $post[$i]->post_content;
$this_post['post_title'] = $post[$i]->post_title;
$this_post['guid'] = $post[$i]->guid;
$id_pot[] = $post[$i]->ID;
array_push($validPosts, $this_post);
}
$post = '';
$i++;
endwhile;
}
return $validPosts;
}
$my_query->post returns the data of a specific post. Instead I had to use $my_query->post*s* to get an array with all the posts fetched as an object.
You are missing a call to the function the_post();:
while ($my_query->have_posts()) :
$my_query->the_post();
$post = $my_query->post;
// ...
endwhile;
See The WordPress Loop