wp reset query not working in admin_notices - php

I'm writing a plugin and I need to show admin notices about the posts. I called to the function which provides messages.
add_action('admin_notices', array($this, 'postNotices'));
And here is my function :
public function postNotices() {
$args = array(
'post_type' => 'demo_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1
);
$query_post = new WP_Query($args);
if ($query_post->have_posts()) {
while ($query_post->have_posts()) {
$query_post->the_post();
// $my_meta = 'Im getting post meta here'; ?>
<div>Some notification</div>
<?php wp_reset_query();
}
}
}
Notices showing is working correctly but When I try to create a new post it appears last post data such as post title in the new post page.
It means wp_reset_query() function is not working with admin_notices hook I think. Has anyone a suggestion?
Any suggestions are highly appreciated.

After hard research I foud the answer,
Here we go,
$args = array(
'post_type' => 'demo_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1
);
$query_posts = get_posts($args);
foreach ($query_posts as $post) {
?>
<div>Some notification</div>
<?php
}
No need to add functions like wp_reset_query(). Found the answer but don't know what the logic is. if anyone know, Please comment here.

Related

custom post type post post_count not working , i prefer display 4 post but now only 1

I made a page template and try to echo the custom post type "products" from template parts
but when I publish the page its only display one post of products only, I prefer display 4 post :(
get_header();
// Reference global $post variable.
global $post;
// Get posts.
$posts = get_posts(array(
'post_type' => 'product',
'post_count' => 4
));
// Set global post variable to first post.
$post = $posts[0];
// Setup post data.
setup_postdata( $post );
// Output template part.
get_template_part( 'template-parts/products' );
// Reset post data.
wp_reset_postdata();
Change your query like this
$query = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 4
);
$loop = new WP_Query($query);
I hope this is helps you
Change your code like this
get_header();
// Reference global $post variable.
global $post;
// Get posts.
$posts = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 4
));
// Set global post variable to first post.
$post = $posts[0];
foreach($posts as $post){
// Setup post data.
setup_postdata( $post );
}
// Output template part.
get_template_part( 'template-parts/products' );
// Reset post data.
wp_reset_postdata();
hope this will helps you
Your query is structured incorrectly as others have mentioned, the best practice I've always known is to use the WordPress loop. Hope this helps!
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 4
);
$query = new WP_Query($args);
if($query->have_posts()):while($query->have_posts()):$query->the_post(); {
// write your code
}
endwhile; endif;
wp_reset_postdata(); ?>

Add meta data from function for orderby in wordpress

Newbee here
I need my post to be orderby by the number of votes from a plugin named wp ulike. but to get the number of votes, I need to run a function. I've tried every way I can think but didn't work.
currently here's my code:
<?php
function votecount(){
if (function_exists('wp_ulike_get_post_likes')):
echo wp_ulike_get_post_likes(get_the_ID());
endif;
}
add_post_meta($post_id, 'votecount', $votecount);
?>
<?php
$ctr = 1;
$args = array(
'post_type' => 'ico',
'posts_per_page' => -1,
'meta_key' => 'votecount',
'orderby' => 'meta_value',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
Any help will be much appreciated.
You have to add filter posts_orderby. Please take reference from below url.
https://wordpress.stackexchange.com/questions/198610/how-to-filter-by-last-name-for-custom-post/198624#198624

How to render formatted post content in widget in Wordpress

I am new on wordpress and try to make a widget which will only show the last post of particuler category.
TO do this I have given some settings in admin to select category for which admin wants to display last post (post of last ID).
I am using the below code to in widget to display post content:
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
echo $post->post_content;
}
But the above code is showing content in one paragraph or you can say that without format. I have done lot of search and found that, I need to apply "the_content" filter to format the content. So I have done the same as below code:
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Now the above changes is returning the null string. I have google lot of things but everyone is saying that to use apply filter or use the_content() function. I have tried both solutions but nothing happening.
Can anyone please share the solution for this problem?
please check whether you are getting any text in $post->post_content
by echo $post->post_content
All you need to do is,
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
while ($posts_query->have_posts()) {
$posts_query->the_post();
echo get_the_content();
}

Wordpress ad_filter to title

i have problem to ad tiitle to title :P
I just have created custom post type. Now i want Get random title from element from custom post type and write it after POST (standard post) here is my code:
function pobierajslowa() {
$args=array(
'orderby' => rand,
'post_type' => 'slowa_kluczowe_wpisu',
'posts_per_page' => 1,
'caller_get_posts'=> 1
);
$queryObject = new WP_Query($args);
while ($queryObject->have_posts()) {
$queryObject->the_post();
return get_the_title();
}
}
function theme_slug_filter_the_content( $title ) {
$custom_content = $title;
$custom_content .= ' '.pobierajslowa().'';
return $custom_content;
}
add_filter( 'the_title', 'theme_slug_filter_the_content' );
After when i use this, i get 503 error :O What i have do wrong?
'orderby' => rand,
Should be...
'orderby' => 'rand', // a string
Make sure to double check syntax. Turning on WP_Debug should tell you where the issues are.

Include several partials in a wordpress template. Showing only one of them

I have built a template and two partials in Wordpress (php). In the template I use get_template_part() two times but only the first one is loaded.
<div class="partial_blockarticle">
<?php
get_template_part('partials/blockarticle');
?>
</div>
<div class="partial_interview">
<?php
get_template_part('partials/interviewpartial');
?>
</div>
In this example, the blockarticles are loaded but no interview. If I switch the divs, only interviews are loaded.
The blockarticles/interviews are Custom Fields and are loaded in the partials like this:
<?php
$pageID = $post->ID;
// Build WP_Query() argument array
$news = new WP_Query(array(
'posts_per_page' => 6,
'post_type' => 'blockarticles',
'order' => 'desc',
'orderby' => 'date',
'meta_key' => 'blockarticle_page_parent',
'meta_value' => $pageID
));
while($news->have_posts()): $news->the_post();
?>
//Do stuff
What am I doing wrong here? $news can not be empty since it shows both articles and interview when I switch place?
Try running wp_reset_query() directly after the endwhile for both of the custom queries. I assume the problem is that the $post->IDin the second include does not return the correct page ID.
Try and call wp_reset_query() before instantiating the new WP_Query instances. Sometimes I've had to do this to get additional WP_Queries working properly.
Also try to echo something right before the new WP_Query in each partial. If it echoes properly, the template loading is working propely. E.g.:
<?php
// Test echo
echo 'does this work?';
$pageID = $post->ID;
// Build WP_Query() argument array
$news = new WP_Query(array(
'posts_per_page' => 6,
'post_type' => 'blockarticles',
'order' => 'desc',
'orderby' => 'date',
'meta_key' => 'blockarticle_page_parent',
'meta_value' => $pageID
));
while($news->have_posts()): $news->the_post();
?>
//Do stuff

Categories