It's a bit more complicated than that.
For a travel website, I have 3 parent categories: Series, Post Type, and Location.
Each post is assigned to a child category. For example (respectively): EU2014, Picture Gallery, and Rome.
On each post, there is a sidebar. My intention is that this sidebar will contain links to the other related posts. A post is related if it is:
1.) In the same Series child category (such as "EU2014")
AND
2.) in the same Location child category (let's say "Rome").
Really what I'm doing here is making it so that way the client can write any number of posts, and a network of links will appear on all of them (in the sidebar).
So, in sum, all posts designated "EU2014" AND "Rome" will be part of a collection.
Then I can format the sidebar nicely based on what Post Type a post in question is (with appropriate icons or whatnot).
My problem is this:
I need pseudo code. I've started working on it, but I'm very concerned that, if I am to iterate through the entire database of posts every time I load a post, simply to write that sidebar, it will be a massive resource drain on the system.
How can I
1.) Identify the child categories of the current post
and
2.) get the names and links of each post whose Series and Location child categories match those of the current post in order to write that information to the sidebar of the current post
without creating a black hole of fuckery when the site grows to more than just a few total posts?
This is going to be written in PHP, so if anyone has any interest in helping me figure out more than just pseudo, I'd be thrilled to suss it out with you.
<?php
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => 8, // specify category id here
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
foreach( $myposts as $post )
{
setup_postdata( $post );
?>
<?php the_title(); ?>
<?php
}
wp_reset_postdata();
?>
For Reference see this
http://codex.wordpress.org/Template_Tags/get_posts
Related
I know, this question has been asked a lot. I have been through a lot of them already, but my issue seems to be more bespoke.
Initially, I had this issue, but seems to be another issue, therefore I am asking a new question in hopes for a solution.
I am using the WP Download Manager Pro plugin that creates a custom post type wpdmpro.
So, the crux of my issue seems to be that my pagination in my loop.php, seems to be connected someone to the default post type and not the custom post type I am using, as in, if I have 0 Posts in my default posts, navigating to /page/2 will not work, but if I have 11 default posts it will, but then /page/3 will not work.
I have 11 wpdmpro posts and 0 default posts. Therefore, I would expect here, that with 2 posts per page, that I would have 6 pages? Wordpress seems to think differently!
Here is my query
<?php
$category = get_queried_object();
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'post_type' => 'wpdmpro',
'wpdmcategory' => $category->category_nicename,
'tag' => $cat_tag
);
// Query
$wpdmpro_query = new WP_Query( $args );
// The Loop
if( $wpdmpro_query->have_posts() ) : while( $wpdmpro_query->have_posts() ) : $wpdmpro_query->the_post();
?>
<!-- Do stuff -->
<?php endwhile; ?>
<!-- Do stuff if there are no posts -->
endif; wp_reset_postdata(); ?>
So WHY does /page/2 return a 404 when that page SHOULD exist? As mentioned, if I start to add posts to the default post (on the 11th post), then /page/2 seems to work, with the 21st post /page/3/ works, so it has to be linked to that. But why? I clearly define that I am NOT using 'post_type' => 'post',. So where could this be defined and how can I override it?
Just a few things to add in case they are somehow linked.
In my permalinks settings, I have a custom structure of
/%category%/%postname%/
and my default category base is .
I have tried resetting the permalinks settings, but still getting the 404 page on /page/2.
In my WP Download Manager Settings, my WPDM Category URL Base is .
So, with all that my url is.
{domain}/category-name/page/2/ which is returning the 404.
{domain}/category-name/ returns 2 posts of the stated category.
Part of me is concerned this is a configuration issue, mixing a custom post type with custom structure and category base. Someone out there must have had this issue? It seems like this should be very simple, but I cannot for the life of me find a solution, perhaps I am looking in the wrong places?
Any help will be massively appreciated.
Edit:
I have tried to regenerate the permalinks on multiple different occassions to no effect.
I am working on a website in Wordpress where I need to use custom posts (which I already created with the help of a plugin).
The problem is that the theme that I use allows me to display the post on the page organized according to categories, but when I create a custom post and put it into a category it is not displayed on the web (as if I had never created the post) but if I create the same post from the normal page of Wordpress entries (a standard Wordpress post type) and I put it in the same category this is shown on the page. Also, when I enter the custom post page the entry I created appears but when I enter the normal entries page it does not appear.
I went to a portal where they said how to add the custom post to the Wordpress categories by writing some lines of code in the functions.php file but this did not work, now I see the custom post within the category page but I still do not see them inside of the Wordpress entries page and also still not shown on the web.
You need to create a custom query. This page has good explanations and examples: https://codex.wordpress.org/Class_Reference/WP_Query
The most important thing in your case is to include this in your arguments array, which selects posts and your CPT:
'post_type' => array('post', 'your-custom-posttype'),
and also this which filters by category:
'category_name' => 'your_category_name'
So a typical simple custom query would look like this:
$args = array(
'post_type' => array('post', 'your-custom-posttype'),
'category_name' => 'your_category_name',
'post_status' => 'publish',
'posts_per_page' => 12
);
$query1 = new WP_Query( $args );
if ( $query1->have_posts() ) {
while ( $query1->have_posts() ) {
$query1->the_post();
echo '<li>' . get_the_title() . '</li>';
// Other stuff echoing content etc. to be added here.....
}
wp_reset_postdata();
}
I have a site where on the home page there are teasers for the 3 most recent posts.
If I make a post sticky then it actually adds a fourth post display and so on.
After searching I found this script which seem to initially work:
<?php
$sticky = count(get_option('sticky_posts'));
$the_query = new WP_Query( 'posts_per_page='. ( 3 - $sticky));
?>
This seemed to do the job perfectly. However I found if one of the posts was a recent post and was also set to sticky then this would affect the amount displyed.
For example, it the second most recent post was made sticky then it would result in only two posts displaying.
Any ideas on how the above code can be altered to always show three posts, sticky or not?
Thanks
$args = array(
'posts_per_page' => 3,
'ignore_sticky_posts' => 1
);
query_posts($args);
-- use while loop here ---
and after that use
wp_reset_query();
What I have:
Home
About
Projects
Project Pages 1
Project 1
Project Pages 2
Project 2
Project Pages 3
Project 3
I would like to loop through the Project Child Pages in order to display the pages in order of most recent of all sub-child projects then be able to echo or pull out Custom Fields ,
<?php the_title();?>
<?php the_excerpt();?>
I do not want actually display the parent pages however, only for example project 1 or Project 2 or Project 3 etc.
Have tried many things but cannot seem to be able to do it with array_merge or wp_list_pages.
Any help would stop me from pulling my hair out. Greatly appreciated.
Read below URL i am very sure it is very help full to you.
How to Exclude Posts from Children Categories on WordPress Category Page
http://thesistut.com/2012/exclude-posts-from-children-category/
I found how to do it. I used the following code. I hope it helps someone.
<?php
$args=array(
'post_type' => 'page',
'post__in' => array(345, 289),
'post_status' => 'publish',
'posts_per_page' => 2,
'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();
?>
This produced 2 posts as pages, from the specific category without displaying the parent, avoiding using wp_list but using a new WP_Query and $args. It worked as a temporary fix but does work.
Is there a way to use THE LOOP in Wordpress to load pages instead of posts?
I would like to be able to query a set of child pages, and then use THE LOOP function calls on it - things like the_permalink() and the_title().
Is there a way to do this? I didn't see anything in query_posts() documentation.
Yes, that's possible. You can create a new WP_Query object. Do something like this:
query_posts(array('showposts' => <number_of_pages_to_show>, 'post_parent' => <ID of the parent page>, 'post_type' => 'page'));
while (have_posts()) { the_post();
/* Do whatever you want to do for every page... */
}
wp_reset_query(); // Restore global post data
Addition: There are a lot of other parameters that can be used with query_posts. Some, but unfortunately not all, are listed here: http://codex.wordpress.org/Template_Tags/query_posts. At least post_parent and more important post_type are not listed there. I dug through the sources of ./wp-include/query.php to find out about these.
Given the age of this question I wanted to provide an updated answer for anyone who stumbles upon it.
I would suggest avoiding query_posts. Here's the alternative I prefer:
$child_pages = new WP_Query( array(
'post_type' => 'page', // set the post type to page
'posts_per_page' => 10, // number of posts (pages) to show
'post_parent' => <ID of the parent page>, // enter the post ID of the parent page
'no_found_rows' => true, // no pagination necessary so improve efficiency of loop
) );
if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
// Do whatever you want to do for every page. the_title(), the_permalink(), etc...
endwhile; endif;
wp_reset_postdata();
Another alternative would be to use the pre_get_posts filter however this only applies in this case if you need to modify the primary loop. The above example is better when used as a secondary loop.
Further reading: http://codex.wordpress.org/Class_Reference/WP_Query