Unable to reset post data in wordpress custom query - wordpress

Edit - I already tried wp_reset_query() it doesn't work, the first loop executes as intended but the second loop just jumps to the else and I get not working
I am using 2 custom WP_Query loops in one page, the first is to get posts from a certain category, and the second is just getting the posts by dates,
here's the code
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
second loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post(); ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php if (the_post_thumbnail()): the_post_thumbnail(); else:echo 'https://lamasec.pythonanywhere.com/static/img/vulnhub.png';endif; ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php
endwhile;
wp_reset_postdata();
else: echo 'not working';
endif;
?>
I am using wp_reset_postdata(); but it doesn't seem to be working.

You should also use:
wp_reset_query()

After reading the documentation for multiple loops in one page here, I only had one WP_Query for both the loops. I stored the ID's of all the posts from the desired category and check for them in the second loop and continue over them. Here's the final code -
first loop
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'any',
'category' => 3,
'posts_per_page' => 4);
$wpdb = new WP_Query($args);
if ($wpdb->have_posts()):
while ($wpdb->have_posts()):
$wpdb->the_post();
$do_not_duplicate[] = $post->ID; ?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php endwhile; ?>
Second Loop
if (have_posts()):
while (have_posts()):
the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<div class="patta p-4 col-lg-3 col-md-6">
<!-- FIX THIS -->
<img class="card-img-top"
src="<?php the_post_thumbnail(); ?>"
alt="<?php the_post_thumbnail_caption() ?>"/>
<h4><b><?php the_title(); ?></b><br></h4>
<p>
Link
</p>
</div>
<?php
endwhile;
endif;
endif;
?>

Related

Pagination in Wordpress after the 5 latest posts

How do I add pagination to this code? It currently shows latest articles.
Pagination can be after every 3 or 5 or 10 posts.
<?php
$the_query = new WP_Query( array(
'category_name' => $category_name,
'offset' => 1, //ignore the first 4 posts and show remaining
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'ignore_sticky_posts' => true,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="home__full">
<div class="row">
<div class="col-md-3">
<small><?php the_time('F jS, Y') ?> • <?php echo reading_time(); ?> • <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></small>
</div>
<div class="col-md-6">
<h3><?php echo substr(get_the_title(),0,95); ?></h3>
<div class="home__latest--excerpt"><p><?php echo substr(get_the_excerpt(),0,140); ?></p></div>
</div>
<div class="col-md-3">
<a href="<?php the_permalink() ?>">
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/blog-thumbnail.png" alt="<?php echo the_title(); ?>" class="wp-post-image" />
<?php } ?>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>

open the wordpress custom post type permalink in modal window

Normally in wordpress permalink will open to a single page. but this time the goal is to open the custom post type permalink in modal window. I have lots of custom post type, and I only like to apply the modal in the doctor custom post type .
how could I achieve this? here is my code:
<div class="slider-wrapper">
<div class="center-doc">
<!--start connect to db-->
<?php $args = array(
'post_type' => 'doctor',
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
);
$posts = get_posts( $args );
foreach ( $posts as $post ): setup_postdata( $post );
?>
<!--end connect to db-->
<div class="slider-box">
<a href="<?php the_permalink(); ?>"> <!-- PERMALINK TO OPEN MODAL-->
<div class="feature-img"> <?php if (has_post_thumbnail()) : ?> <?php the_post_thumbnail(); ?>
<?php else : ?> <img src="<?php echo get_template_directory_uri(); ?>/img/default-img.png" alt="<?php the_title(); ?>">
<?php endif; ?><!--/pro pic-->
</div>
<h6><?php the_title(); ?></h6>
<div class="content"><?php the_excerpt(); ?></div>
</a>
</div>
<!-- MODAL WINDOW TO OPEN WHEN PERMALINK CLICKED-->
<div id="modal" class="modal-window">
<div>
<i class="fa fa-times-circle" aria-hidden="true"></i>
<div class="detail-wraper">
<div class="detail-info">
<h3><?php the_title(); ?></h3>
<div class="details">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
I am using Wordpress 5.9.3 version.

WordPress get posts by category_name

I have a CPT in the single-portfolio.php file where I would like to list blog posts according to their category.
That is, the items I have in my CPT called Portfolio are "India", "USA", "Mexico", "Spain" and "Italy". The blog categories are exactly the same.
How can I get the news in the India page to return to me with the India category? And on the USA page the news with category USA...etc
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'usa'
);
$query = new WP_Query( $args );
?>
<section class="news-slider">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="slider-container">
<div class="slick-slider">
<?php while( $query->have_posts() ) : $query->the_post() ?>
<div class="slick-slide" style="background-image:url(<?= the_post_thumbnail_url() ?>)">
<a class="permalink" href="<?= the_permalink() ?>"></a>
<div class="title-container">
<span class="meta-category"><?php the_category( ', ' ); ?></span>
<h3 class="title"><?php the_title() ?></h3>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
</div>
</section>
If I understood your question, you are trying to show regular Posts on your CPT post where the name of the CPT Post is the same as the posts category name.
If that's the case you can do this (new lines are commented):
<?php
// Get the post object;
global $post;
// Get the post_name (e.g. slug) of the post
$slug = $post->post_name;
// As long as the slug of your CPT post is the same as the slug of your post category, this works.
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
// Use the $slug variable here.
'category_name' => $slug,
);
$query = new WP_Query( $args );
?>
<section class="news-slider">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="slider-container">
<div class="slick-slider">
<?php while( $query->have_posts() ) : $query->the_post() ?>
<div class="slick-slide" style="background-image:url(<?= the_post_thumbnail_url() ?>)">
<a class="permalink" href="<?= the_permalink() ?>"></a>
<div class="title-container">
<span class="meta-category"><?php the_category( ', ' ); ?></span>
<h3 class="title"><?php the_title() ?></h3>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
</div>
</section>

Can't display custom post type categories on the page

I have a custom post type called "case_studies" and it has 3 categories: "seo", "website-design" and "facebook-advertising".
I have several posts in the main page, and for each post I want to output what categories the post has.
For example, the latest post has a category of "seo" and "facebook-advertising", and I want it to be displayed in the page.
I have tried the get_the_terms(); function but I think I am using it incorrectly.
Here is the code of the page:
<div class="container">
<!--
<div class="category_container">
<p class="category_item" id="all">All</p>
<p class="category_item" id="website">Websites</p>
<p class="category_item" id="facebook">Facebook Ads</p>
<p class="category_item" id="seo">SEO</p>
</div>
-->
<div class="row d-flex">
<?php
$args1 = array( 'post_type' => array('case_studies'), 'order' => 'DESC', 'posts_per_page' => 30, 'orderby' => 'date' );
$loop = new WP_Query( $args1 );
while ( $loop->have_posts() ) {
$loop->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($loop->ID));
?>
<div class="col-sm-4">
<div class="case-study-content">
<a href="<?php the_permalink() ?>" class="blog_blocks">
<div class="b_image">
<img src="<?php echo get_the_post_thumbnail_url(); ?>"/>
</div>
<div class="b_h_sec">
<h2><?php the_title(); ?></h2>
<p><?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?></p>
<span class="r_m">Read More</span>
</div>
</a>
</div>
</div>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
Here is how the output looks right now and where I want categories to be displayed.

WP Query Multiple Shortcodes not working on same page or template page

I created a Shortcode slider which gets ids of different pages and show display slider. Shordcode works fine but issue is that whenever i am copy past shortcode multiple times on same page/ template page it shows only first one.
This issue occurs only only when i past same type of shortcode but if i past any other shortcode on same page it works fine.
Here is my code
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is shortcode
[objectx-pages-list id="15,16,17"]
[objectx-pages-list ids="25,26,27"]
here you can see live example
http://objextheme.wpengine.com/
This one working fine
ROOFTOP PATIO & LOUNGE
but this is not working
This Week At Vertigo Sky Lounge
Please guide me where i am doing mistake. Thanks
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is the error i noticed. id="carousel-rooms" Id repeating on
same page. that is why only one time it runs perfect.

Resources