Try this code. Example below
$the_slug = 'my-slug';
$args=array(
'orderby' =>'parent',
'name' => $the_slug,
'order' =>'asc',
'post_type' =>'page',
);
Yes, wonderful – I can do it with one slug, but I’m after multiple slugs. Perhaps I’m just not doing that part right as this code breaks the page…
<?php
$the_slug = 'food', 'cocktails', 'wine';
$args=array(
'orderby' =>'parent',
'name' => $the_slug,
'order' =>'asc',
'post_type' =>'page',
);
$page_query = new WP_Query($args);
?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<div class="teaser">
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
Yes, it allows only one slug at a time, so you have to run this code multiple times to get the result you want and that’s not a good way of coding. But I wonder,why didn’t you try to set some specific tag or custom fields for those posts you want and then try to retrieve them by the same tag or custom field? so that there would be lot of options available.
Hi Hakkim – I really don’t know how to do any of this. I’m not a developer. I know what I’m after, and then when it comes to how to do it, I just scour the web looking for a snippet of code.
For every post/page you would get an option from admin side to set a tag or custom field in its edit page. You can use multiple tags or custom fields for a single post. Later you can use the same snippet of code by changing the arguments values.
For ex:
$args=array(
'orderby' =>'parent',
'tag' => 'apples+oranges',
'order' =>'asc',
'post_type' =>'page',
);
query_posts($args);
so that you can fetch all posts having the specified tags. Note that multiple tags are separated by ‘+’ sign.
Okay, I enabled tagging for pages by grabbing some code here and slapping it in my functions.php. I tagged each page with the corresponding name, so just like the slug names. Then I tried to add in your code with the updates to the tag names and it broke the page. Again, lack of understanding for how this works. Sorry. This is the code i’m trying to use now…
<?php
$args=array(
'orderby' =>'parent',
'tag' => 'food+cocktails+wine',
'order' =>'asc',
'post_type' =>'page',
);
query_posts($args);
?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<div class="teaser">
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
Sorry, I was using this….
<?php
$args=array(
'orderby' =>'parent',
'tag' => 'essen+trinken+wein',
'order' =>'asc',
'post_type' =>'page',
);
query_posts($args);
$page_query = new WP_Query($args);
?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<div class="teaser">
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
Remove the comma after ‘post_type’ =>’page’ and try again. Also have you set tags for posts from admin side and save it? if not please do that and use the same tag name in the above code.
Thanks for sticking with me, Hakkim. I really appreciate it.
This is currently the code I’m using, after having removed the comma after ‘post_type’ =>’page’
It seems to only work with a single tag. If I add more than one, it stops working and nothing displays, although it doesn’t break the page.
<?php
$args=array(
'orderby' =>'parent',
'tag' => 'essen+trinken+wein',
'order' =>'asc',
'post_type' =>'page'
);
query_posts($args);
$page_query = new WP_Query($args);
?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<div class="teaser">
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
Try this way
$args=array(
‘orderby’ =>’parent’,
‘tag_slug__in’ => array(‘essen’,’trinken’,wein’),
‘order’ =>’asc’,
‘post_type’ =>’page’
);
and remove this line query_posts($args);
In this case, would it be looking for a page that is tagged, ‘essen’, and ‘trinken’ and ‘wein’?
Sorry. I’ve realized I can just tag all three pages with ‘menu’ and as they are set to order anyway, it really achieves what I’m after.
That said, ideally, it would be great to be able to have an arbitrary order for design purposes. But this achieves what I’m after, so I do thank you. The reason I was after a way to display pages by slug was so I could just list them in the order I wanted them to appear, along with not having to fuss over an ID as they are often if ever in sync unless the databases are exactly the same between what’s online and my local machine where I build the sites.
So thank you for your help. We can leave it there if you like. Perhaps some day I’ll achieve my dream. 🙂
Ah, just saw your new post. I’ll try it.
Boom! That works! THANK YOU! Now I have two very useful bits of code. Lots of love from Germany, Hakkim!
For anyone’s future reference, the code works in both manners as you would assume…
<?php
$args=array(
'orderby' =>'parent',
'tag_slug__in' => array('food','cocktails','wine'),
'order' =>'asc',
'post_type' =>'page'
);
$page_query = new WP_Query($args);
?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<div class="teaser">
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
or…
<?php
$args=array(
'orderby' =>'parent',
'tag_slug__in' => array('menu'),
'order' =>'asc',
'post_type' =>'page'
);
$page_query = new WP_Query($args);
?>
<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
<div class="teaser">
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
So, a nice use for this code could be displaying pages in a single template, like if you have a menu with three sections (food, cocktails, wine). So you can tag them all “menu” and use just that tag in the code, or you can control the order in an arbitrary way by listing the tags in the array.
Thank you again to Hakkim. I’m really happy to know there are kind people out there, willing to help. It’s really cool.
You’re welcome man…:) Enjoy !!!