I have setup an custom post type with the structure below:
Main post
Children post
Sibling post
I have two main posts called "Vrouwen" en "Mannen"
When i visit one of these main posts i would like to only display the siblings.
Im stuck in achieving this.
But then the "children" are also displayed.
I need to go one level deeper somehow.
All help is appreciated!
I tried the code below.
$mysibling = $post->post_parent;
$mychild = $post->ID;
$mychildmysibling = array( $mychild, $mysibling );
$args = array(
'post_parent' => $mychildmysibling,
'post__not_in' => array( $post->ID ),
'posts_per_page' => -1,
'post_type' => 'collectie'
);
$parent = new WP_Query( $args );
while ( $parent->have_posts() ) : $parent->the_post();
But then the "children" are also displayed. I need to go one level deeper somehow.
First of all post_parent expects a number but you set an array. Secondly, you basically need to make it work only for the main pages? So query should look like this:
// find children for the main post
$children = get_children( array('post_parent' => $post->ID));
// check if the post has any children
if ( ! empty($children) ) {
// get all posts where the parent is set as children to main post
$args = array(
'post_parent__in' => array_keys($children),
'posts_per_page' => -1,
'post_type' => 'collectie'
);
$siblings = new WP_Query( $args );
if ( $siblings->have_posts() ) {
while ( $siblings->have_posts() ) {
$siblings->the_post();
echo get_the_title();
}
}
wp_reset_postdata();
}
Related
Our current website is using custom posts with parent/child posts.
When viewing a (parent) post, a plugin is used to pull its child posts and those are displayed in a tab on the page.
We are using a new version of that custom theme on several websites now and are no longer using parent/child relationship. Instead we have metaboxes in our custom posts types and all additional information can be filed right there.
I wanted to update this particular website with the latest version of the theme, but since it's using parent/child relationship I wanted to add a few lines of code to the theme in order to achieve the same result and keep the old posts the way they are instead of modifying them all.
Here's what I want to do : I want a very simple way to display all child post (in order) on a parent post page.
I've found a few ideas here and there but none seems to be working so far. (Here's one for example : http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/ as well as this one https://wordpress.stackexchange.com/questions/153042/how-to-display-list-of-child-pages-with-parent-in-wordpress). I don't know if it has to do with the fact that i'm using post rather than pages.
I don't want a list of the child post but rather display the content of those directly. Was thinking the best way to achieve this might be to create a function to retrieve the child post and then echoing the result in the template. That way without having to change our theme it could work with our different website.
EDIT :
So far here's what i've tried within single.php :
$query = new WP_Query( array(
'post_parent' => get_the_ID(),
));
while($query->have_posts()) {
$query->the_post();
the_content(); //Outputs child's content as it is
}
wp_reset_query();`
I then changed the code to :
$new_args = array(
'order' => 'ASC',
'post_parent' => get_the_ID()
);
$new_query = new WP_Query( $new_args);
if ($new_query->have_posts() ) {
while($new_query->have_posts() ) {
$new_query->the_post();
the_content();
}
wp_reset_query();
}
and then since it didn't work either i've changed it to :
$children = get_children( array('post_parent' => get_the_ID()) );
foreach ( $children as $children_id => $children ) {
the_title();
the_content();
}
The latest seems to be able to return some results, it "knows" that the current post has children in it but i'm displaying the title and content of the current post. I'm pretty sure I shouldn't be using the_content() in here.
Ok, try something like this inside your post template within the loop. It should help you to output child posts inside particular post.
<?php
/Somwhere in the loop/
$query = new WP_Query( array(
'post_parent' => get_the_ID(),
'posts_per_page' => 3, //shows only 3 children. If you want to show all of them, comment this line
));
while($query->have_posts()) {
$query->the_post();
/*Output the child markup here*/
the_content(); //Outputs child's content as it is
}
wp_reset_query();
?>
UPDATED:
Ok, you can try to use post_parent__in & array of IDs, this should work too.
<?php
/Somwhere in the loop/
$query = new WP_Query( array(
'post_parent__in' => array(get_the_ID()),
'posts_per_page' => 3, //shows only 3 children. If you want to show all of them, comment this line
));
while($query->have_posts()) {
$query->the_post();
/*Output the child markup here*/
}
wp_reset_query();
?>
If not, here is the way to output contents of the posts you got using get_children function. This should probably work too
<?php
$children = get_children( array('post_parent' => get_the_ID()) );
foreach ( $children as $children_id => $child ) {
echo $child->post_title;
echo str_replace( ']]>', ']]>',apply_filters( 'the_content', $child->post_content )); //mimic the_content() filters
//echo $child->post_content; // if you do not need to filter the content;
}
?>
/* Fetch only one level child pages of a parent page */
$pages = get_pages(
array (
'parent' => 'parent_id',
'post_type => 'page',
'post_status' => 'publish',
);
$ids = wp_list_pluck( $pages, 'ID' );
/* Return page IDs in array format */
/* You can retrieve "post_title", "guid", "post_name" instead of "ID"
/* Fetch all child pages of a parent page*/
$pages = get_pages(
array (
'parent' => '-1',
'child_of' => 'parent_id', /* Return child of child for current page id */
'post_type => 'page',
'post_status' => 'publish',
);
$ids = wp_list_pluck( $pages, 'ID' );
/* Return page IDs in array format */
/* You can retrieve "post_title", "guid", "post_name" instead of "ID" */
So on my template for taxonomy-product_tag.php, I want to get all product id's from the Category.
Here is how I currently do it
<?php
$post_ids = array();
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'dog-collars', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$post_ids[] = get_the_ID();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_query();
print_r($post_ids);
?>
I can loop through the product_cat, pull id's into an array and then further down the page I use foreach and the WC product factory to manipulate data how I want it shown for users.
My problem is I need the loop to be Dynamic based on categories, and I can't understand how to do this.
I did think I can just grab the category name from the url
<?php $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
Grab it and the parse to just get the last , i.e category name, and then print into loop
But this seems like it would be a really poor way of doing it.
What I want is in the args
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'DYNAMICHERE', 'orderby' => 'rand' );
I want to be able to populate product_cat dynamically based on the category I am on
Any help or advise / pointing me in the right direction would be appreciated
Use get_query_var( 'product_cat' ).
I am trying to display random child pages of a parent page, but I get random posts which I dont want to be included in the display area.
$my_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1', 'pagename=guide') );
So, what I want is to display random child pages of a parent page who slug is guide, but instead I get random posts which is quite different from what I want. Any help would be appreciated. Thanks :)
This is working for me. the post_type is the important part since otherwise it doesn't seem WP will query against the pages. The post_parent should be the integer id of your page.
$query = new WP_Query( array( 'orderby' => 'rand', 'posts_per_page' => 1, 'post_type' => 'page', 'post_parent' => '2' )) ;
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
This worked for me:
$my_wp_query = new WP_Query(); // Setup query object
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page')); // Get all pages
$children = get_page_children( get_the_id(), $all_wp_pages); // Get the children pages from list of all pages
$rand = rand(0, (count( $children) - 1)); // Count children and get a random number from that range
print_r( $children[$rand]); // Print random child object
Hope this helps!
First of all - this is a question mainly about the loop™. I'm also having problems with the attachment thing, but that is somehow secondary since there are snippets around there I think could be useful.
Ok, so this is want I want to do in a front page:
get 7 posts from custom post type 'portfolio'
only with the first one, get an specific attachment (in any possible way, be it filename, order in the media manager... whatever is the best, more clean way)
with the other 6, just get the_post_thumbnail() and little more.
Now, I have this:
<?php
$args = array (
'post_type' => 'portfolio',
'order' => 'DESC',
'posts_per_page' => 7
);
$query = new WP_Query( $args );
$first_post = true; /* we will take the latest work first */
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
if ($first_post):
$first_post = false;
?>
<div> /* This is the div for the first item */
<?php /* let's take the first attachment */
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'order' => 'DESC'
);
$attachments = get_posts( $args );
if ( $attachments ) :
foreach ( $attachments as $attachment ) :
echo wp_get_attachment_image( $attachment->ID, 'full' );
endforeach;
endif;
?>
</div>
<?php else: ?>
/* Do something with the other 6 posts and their post_thumbnail */
<?php endif ?>
<?php endwhile; ?>
And now for the questions:
First and foremost: if I set 'numberposts' to all (-1) when trying to recover the attachment, I get ALL attachments from ALL 'portfolio' posts. Shouldn't I be interacting only with the current post (the_post())? I can't quite grasp the concept of the loop here, this is the main question.
That code won't get me the first attachment, even if it's placed in the first place in that post's media manager.
Should I go for secondary or nested loops? I've read and re-read the codex and other tutorials but still can't wrap my head around it.
Many thanks!
used this code :
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
}
}
User Shakti Patel gave me the key to the answer, but didn't really answer my question about the loop so here it goes:
The problem was with get_posts. It actually runs a parallel query to the main one without taking into account the current step of the loop. So we have to ask it for the attachments of the current post, which since we're in the loop is stored within $post->ID. So knowing that, we have to request the first attachment for the current post like this:
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);
$attachments = get_posts( $args );
That way we specify what is the post from which we'll get the first attachment, and while we're on it, exclude the post thumbnail.
I don't know if this is the best way since we're already on a loop and we shouldn't need a new query, shouldn't we be able to retrieve that attachment without the get_posts bit?
Anyway, for more info on get_posts (read while not half asleep): http://codex.wordpress.org/Template_Tags/get_posts
I've been searching high and low for and answer to this, but I'm not actually sure it's possible!
I have a WP_Query that pulls posts from almost everything, however, I wish to exclude a specific category and/or all it's sub categories.
Searching around people are yet to find a solution for this.
Here's my query so far:
$args = array(
'post_type' => 'sell_media_item',
'cat' => -98,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
I thought just excluding cat 98 would grab all the sub categories too, but apparently not.
I've tried using:
category__not_in, depth=0, parent=0 and even an adaptation of this, with no luck.
Any ideas?
[EDIT]
I'm using a custom taxonomy called Collections, so putting 'collection' => 'vip' into the query means it will only show this collection. I'm thinking if there's a way of reversing this so it excludes the collection instead?
As it's not possible to list all of the categories that will appear here as they will be changing all of the time.
[EDIT 2]
After the discussion in the comments below, here's the updated code.
$ex = array(
'taxonomy' => 'collection',
'child_of' => 98,
'hide_empty' => 0
);
$categories = get_categories($ex);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
echo('<pre>'); var_dump($categories);
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php echo('<br /><pre>'); var_dump($args); ?>
<?php $loop = new WP_Query( $args ); ?>
I would get the list of all sub categories with get_categories() and then build a 'cat' exclusion array based on the results.
$args = array('parent' => 98);
$categories = get_categories($args);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
This is just an example, you may have to modify it slightly to fit your needs.
So!
It appears I was trying to do the impossible. I couldn't get this script working for the life of me. So I tried a different angle. Instead of excluding a custom taxonomy and its terms, I decided to move all of my other terms into a parent term and just called that instead.
Here's the code if anyone's interested...
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'sell_media_item',
'taxonomy' => 'collection',
'term' => 'clubs',
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ($loop->have_posts()) : $loop->the_post(); ?>
I wrote my own function in order to exclude subcategory posts from the loop, using tips from the above post and elsewhere.
In my theme archive.php file, above the loop, I list the subcategories (optional):
<?php
$current_cat = get_queried_object();
$args = array( 'parent'=>$current_cat->term_id, 'child_of' => $current_cat->term_id, );
$categories = get_categories( $args );
foreach($categories as $category) { ?>
<h2><?php echo $category->name ;?></h2>
<p> etc....</p>
<?php } ?>
In my functions.php file, I've added the following custom function using pre_get_posts:
add_action( 'pre_get_posts', 'main_query_without_subcategory_posts' );
function main_query_without_subcategory_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_category() ) {
//Get the current category
$current_category = get_queried_object();
//get the id of the current category
$current_cat_id = $current_category->term_id;
//find the children of current category
$cat_args = array( 'parent'=>$current_category->term_id, 'child_of' => $current_category->term_id, );
$subcategories = get_categories( $cat_args );
//Get a list of subcategory ids, stick a minus sign in front
$subcat_id = array();
foreach($subcategories as $subcategory) {
$subcat_id[] = " -". $subcategory->term_id;
}
//join them together as a string with a comma seperator
$excludesubcatlist = join(',', $subcat_id);
//If you have multiple parameters, use $query->set multiple times
$query->set( 'posts_per_page', '10' );
$query->set( 'cat', ''.$current_cat_id.','.$excludesubcatlist.'' );
}
}
}
Then in the archive.php, below the subcategories, I've added the regular WordPress loop which is now being modified by the above function:
<?php while (have_posts() ) : the_post(); ?>
<h2><?php the_title();?></h2>
<p> etc....</p>
<?php endwhile;?>
Though the WordPress codex says that using "category__in" will exclude posts from subcategories, that didn't work for me and subcategory posts were still showing.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
https://developer.wordpress.org/reference/hooks/pre_get_posts/