Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query custom posts by page slug WordPress

  • SOLVED

Hi, i'm looking to query a custom post type (projects) by taxomony (projectcat), the code i found below works fine for regular posts, but not custom post types when i have tried to modify it.


<?php
if ( is_page() ) {
$page_name = $posts[0]->post_name; //or use $posts[0]->post_title
$category = get_term_by('slug',$page_name,'category');
if ($category) {
$args=array(
'category__in' => array($category->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'showposts' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h4>Related News</h4>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php
endwhile;
}
}
}
wp_reset_query();
?>


Alternatively i'd be happy to query custom posts on a page by use of a custom field.

Thanks in advance!

Answers (4)

2011-04-26

David Navarrete answers:

hi try with this
<?php
$query = new WP_Query(array('post_type' => 'projects', 'projectcat' => $page_name));
?>


Mike comments:

Got it working, thanks

<?php

$page_name = $post->post_name;

$query = new WP_Query(array('post_type' => 'projects', 'projectcat' => $page_name));

if( $query->have_posts() ) {

echo '<h2>Projects</h2>';

while ($query->have_posts()) : $query->the_post(); ?>

<div class="project">
<a href="<?php the_permalink() ?>" class="image"><div class="sml"><?php the_post_thumbnail( 'newsthumbnail' ); ?></div>
<div class="text">
<h2><a href="<?php the_permalink() ?>"></a><?php the_title() ?></h2>
<?php $more = 0;the_content('Read more'); ?>
</div>
</div> <?php

endwhile;

}

wp_reset_query();

?>

2011-04-26

Daniele Raimondi answers:

to query a CPT classified under a particular term of your custom taxonomy, you can try:

$args = array(
'post_type' => 'projects'
'taxonomy' => 'projectcat',
'term' => '---a term of projectcat taxonomy---',
'orderby' => 'date', //that's default
'order' => 'DESC', //that's default
'post_status' => 'publish',
);
$myloop = new WP_Query($args);?>



If you wanna query using a custom field, take a look [[LINK href="http://codex.wordpress.org/Function_Reference/WP_Query"]]here
[[/LINK]] @Custom Field Parameters

For example, if you have a numeric CF (say "price") and wanna test it against a min and max value you can add to the args above, a piece of code like this

$args['meta_query']= array(
array(
'key' => 'price',
'value' => array( $min, $max ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'price',
'value' => 1,
'type' => 'numeric'
)
);

2011-04-26

S├ębastien | French WordpressDesigner answers:


<?php
if ( is_page() ) {
$page_name = $post->post_name;
$category = get_term_by('slug',$page_name,'projectcat');
if ($category) {
$args=array(
'category__in' => array($category->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'showposts' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h4>Related News</h4>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php
endwhile;
}
}
}
wp_reset_query();
?>

2011-04-26

John Cotton answers:

For a start you need to change the lin

'post_type' => 'post'

to

'post_type' => 'projects'

After that, you might need to change the category arguments. Is you custom taxonomy hierarchical or not?

John


Mike comments:

Hi John,

I'd given that a go with out success, may be my mistake, the taxonomy was hierarchical, thanks fot the reply