I have this post type that I want to loop through. I have to create two different sections. The top works where it loops through all. But I want to exclude the parent that's holding the children (#6474) and everything in that parent to loop in a different row.
What I have so far. This works in regards of posting all posts. But at the moment includes all parent and children minus #6474. Just want this to show ones that are only parents.
Trying to figure out how to approach creating another row that will only show the children in the post type.
$customersPage_args = array (
'post_type' => array( $global_cat ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'publish_date',
'post__not_in' => array(6474) //excluding the ID holding the children
);
$global_cat_query = new WP_Query( $customers_sort ); ?>
<h3 class="h2 display <?php echo $block[className]; ?>"><?php echo $block_heading; ?></h3>
<div class="card-row">
<div class="card u-pb-0">
<div class="row">
<?php // The Loop
if ( $global_cat_query->have_posts() ) :
while ( $global_cat_query->have_posts() ) : $global_cat_query->the_post(); ?>
<div class="col-md-3 col-sm-4 col-6">
<a href="<?php echo get_permalink(); ?>">
<div class="card card u-mt-0 u-mb-4 align-items-center">
<img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" />
</div>
</a>
</div>
<?php endwhile;
endif;
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php endif; ?>
If you only want top level items, you can make use of the post_parent parameter. If you set it to 0, it will only find "parent" (aka "Top-Level" posts):
$customersPage_args = array (
'post_type' => array( $global_cat ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'publish_date',
'post__not_in' => array(6474), //excluding the ID holding the children
'post_parent' => 0, // Only get "Top Level" posts
);
You can use this function to detect if a post has a parent:
https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/
Related
I'm developing a simple wordpress site. I've created a home page named home.php and trying to dynamically display posts separately according to category. the code is the following:
<?php
get_header();
?>
<main id="main" class="row">
<?php
$categories = ['politics' => 'রাজনীতি', 'finance' => 'অর্থনীতি', 'sports' => 'ক্রীড়া', 'entertainment' => 'বিনোদন', 'fenii' => 'আমাদের ফেনী', 'religion' => 'ধর্ম', 'literature' => 'সাহিত্য', 'study' => 'পড়া-লেখা', 'quiz' => 'কুইজ', 'zodiac' => 'রাশিফল'];
foreach($categories as $key => $category):
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'ignore_sticky_posts' => true,
'category_name' => $category,
);
$query = new WP_Query( $query_args );
?>
<section id="<?php echo $key ?>" class="col-sm-6 section">
<h3 class="title"> <?php echo $query->query['category_name'] ; ?> </h3>
<?php
if ( $query->have_posts()):
while ( $query->have_posts() ):
$query->the_post();
?>
<h5> <?php the_title() ?> </h5>
<p><?php the_excerpt() ?></p>
<?php
endwhile;
echo "<hr />";
wp_reset_postdata();
else:
echo "<h6> Sorry! No post has been found of this type. </h6>";
echo "<hr />";
endif;
?>
<hr />
</section>
<?php
endforeach;
?>
</main>
<?php
get_footer();
?>
But, no post is displayed like the following:
So, how can I display my posts according to category dynamically by foreach loop as I've been trying since the beginning?
Thank you.
Have you tried using category_id as the argument for category instead of category_name in your $query_args?
Also, you should get your categories dynamically instead of creating the array for categories manually. Otherwise, you'll have a problem if you add or remove categories in the future. Have a look at https://developer.wordpress.org/reference/functions/get_categories/.
I'm developing a "real estate sale campaign", you can check it here. I've created a loop for each apartment/house on WordPress, I've made a page theme. On the page, you can check that I have a "neighborhood" section, where I want to display how many apartments I have in each "category", like Bela Vista, Boa Vista and others.
The problem is that I don't know how to do that. Here is the code I use to loop the apartments when the fields are filled:
<?php
$newsArgs = array(
'post_type' => 'property',
'posts_per_page' => 200,
"orderby" => 'meta_value_num',
"meta_key" => 'numerooff',
"order" => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'property-status',
'field' => 'slug',
'terms' => 'oneoff',
)
)
);
$newsLoop = new WP_Query( $newsArgs );
while ( $newsLoop->have_posts() ) : $newsLoop->the_post();?>
Maybe I should copy something of this code, to display the numbers? I don't know really. Here is where I create the slug I'm trying to use. On the snippet above, you can see where I want to place it:
<!-- |-----------BELA VISTA-------------| -->
<div class="col-sm-3 col-xs-12 portfolio-item">
<a href="http://www.onecia.com.br/oneoff-belavista" target="_blank" class="">
<div class="view efffect">
<div class="portfolio-image">
<img src="<?php bloginfo('template_directory');?>/acoes/oneoff/bairro/belavista.jpg" alt=""></div>
<div class="mask text-center">
<h3 style="font-family: 'Novecento Wide Light'; color: #fff;">Bela Vista</h3>
<h4 style="color: #fff;">I WANT TO DISPLAY IT HERE</h4>
</div>
</div>
</div></a>
What should I do?
get_term() and get_category() functions return an object which has count value.
So you can use such script:
<?php
$cat_by_id=get_term(TERM_ID_HERE); // get_category(CAT_ID_HERE)
//$cat_by_slug= get_term_by('slug', 'oneoff-belavista', 'property-status');
?>
<h4 style="color: #fff;"><?php echo $cat_by_id->count;?></h4>
Another easy way to get posts count is to use found_posts hook:
<h4 style="color: #fff;"><?php echo $newsLoop->found_posts; ?></h4>
After $newsLoop = new WP_Query( $newsArgs ); use $count=$newsLoop->found_posts; echo $count;
I have Website with WordPress Bootstrap:
http://www.obra-93.hr/zavrseni-projekti/
I have portfolio with items per row.
The code for loop 3 items per row (totaly 20 items) is below:
<div class="container">
<div class="row vrow">
<?php // slideshow
$args_projekti = array(
'showposts' => -1,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
'post_type' => 'projekti',
'post_status' => 'publish',
'cache_results' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false
);
$projekti = new wp_query($args_projekti);
$p=0;
if ($projekti->have_posts()):
?>
<?php
while ($projekti->have_posts()):
$projekti->the_post();
$post_id = get_the_ID();
?>
<div class="col-xs-12 col-sm-4 col-md-4">
<?php the_post_thumbnail('projekt-thumb', array('class' => 'img-responsive fade center-block'));?>
<h3 class="text-center"><?php the_title(); ?></h3>
</div>
<?php if($p%3===0){echo '</div><div class="row vrow">';} ?>
<?php $p++; endwhile; endif; wp_reset_query(); ?>
</div>
</div>
My first and last row is not working properly. Why?
Thanks!
if you increment $p after the if condition the values would be like
0,1,2
3,4,5
and so on an because 0 % 3 == 0
so you could start p with 1 then it looks like
1,2,3
4,5,6
or increment before the check
So this is my first post and wow, I didn't know this site existed. I've had a look around at questions and I hope that mine isnt a dumb nooby one. Although I am a noob :S
Ok, so I created a function in WordPress that will add a meta box to the new posts page so that I can specify whether or not this post should be featured (I read this is better than creating a featured category for SEO purposes?).
Anyway.. The code I have works in showing the most recent. Here is the code for that:
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$custom = get_post_meta($my_query->post->ID, '_featuredpost_meta_value_key', true);
if ( $custom ){
?>
<article class="container" itemprop="blogPosts" itemscope itemtype="http://schema.org/BlogPosting">
<div class="row">
<h2 itemprop="about">
<?php the_title(); ?>
</h2>
</div>
<div class="row">
<div class="<?php if ( has_post_thumbnail() ) { ?>two-thirds column<?php } else {?> twelve columns <?php } ?>">
<p class="post-excerpt"><?php modified_excerpt(); ?></p>
</div>
<?php if ( has_post_thumbnail() ) { ?>
<div class="one-third column">
<?php the_post_thumbnail('full', array('class'=>'hide-mobile')); ?>
</div>
<?php } ?>
</div>
<div class="row">
Continue Reading
</div>
<hr />
<div class="post-info">
<ul>
<li class="date"><?php the_date();?></li>
<li class="author"><?php echo get_the_author_meta('display_name'); ?></li>
<li class="category"><?php the_category(', '); ?></li>
<li class="tags"><?php the_tags('',', ',''); ?></li>
</ul>
</div>
</article>
<?php
}
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Now, When I use the same code below, but then use:
if ( ! $custom ){
to show the posts that are not set to be featured that also works. The problem is that the pagination no longer works. When I go to the second page it just duplicates what is on the home page.
This leads me to believe that I have created a mashed together crappy bit of code. Can someone please help me build a loop, that will exclude any posts where the meta data _featuredpost_meta_value_key is set to Yes.
Thanks in advance
You'll want to use a WP Meta Query in your original $args array.
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
From the docs, here's an example:
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
But you can also use the sugar provided by the WP_Query class and pass it in as the meta_query value to your original args:
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'meta_query' => array(
'key' => '_featuredpost_meta_value_key',
'value' => 'Yes',
'compare' => '='
)
);
I'm developing my first Wordpress theme and the first loop I have is only outputting 1 item: a link to the homepage (not any of the arguments I am trying to pass in the array).
Here's the php and html:
<div class="services_list">
<?php
$args = array(
'posts_per_page'=> 999,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'service',
'meta_key' => 'featured',
'meta_value' => '1'
);
// The Query
get_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="service_item">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="service_top_link">
<div class="service_image"><?php the_post_thumbnail( array(120,120) ); ?></div>
</a>
<h3 class="service_title"><?php the_title(); ?></h3>
<div class="service_excerpt"><?php the_excerpt(); ?></div>
Learn More
</div><!-- .service_item -->
<?php endwhile;
// Reset Query
wp_reset_query();
?>
</div><!-- .services_list -->
I apologize if this question has already been answered, but I can't seem to find anything on it.
worked! thanks. I switched from get_posts to using WP_query and was having the same problem. Turns out the issue was actually with the meta value, not the query itself.