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

Custom fields and post types query WordPress

I have two custom post types…

BOOK and BOOK AUTHOR.

I want to…

LIST all BOOK's by an author on their respective AUTHOR PAGE.

So if i have 10 books by Stephen KING. I want to list them all (and only those by him) on the STEPHEN KING page. I am having real trouble working out how to query posts to do this.

Any advice? I am using advanced custom fields plugin if that helps, but can't work out how to query and display this post information.

I currently use the following code to display ALL of my books, but how do i get the specific ones on their specific author pages?

<?php
$args=array(
'post_type' => 'book',
'post_status' => 'publish',
'posts_per_page' => 12,
'caller_get_posts'=> 1,
'orderby'=> 'date',
'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '';
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
if($i % 6 == 0) { ?>
<div class="row">
<?php
}
?>

<img src="<?php the_field('cover_image'); ?>" /><br />
<?php the_field('author_name'); ?><br />
<?php the_title(); ?><br />

</div>

<?php
if($i % 6 == 0) { ?>

<?php
}

$i++;
endwhile;
}
wp_reset_query();
?>


The AUTHORS are being defined with Wordpress Categories if that helps.

Answers (5)

2014-02-09

Remy answers:

What values are your storing with ACF ? If it's two Custom post types, you might want to use the post2post plugin to help you create relations between them


lowercase comments:

The AUTHORS are being defined with Wordpress Categories if that helps.


Remy comments:

Are authors a custom taxonomy ? what is its name ? Assuming it is, and that you are on an author page, you can use this code :


<?php

$args=array(

'post_type' => 'book',

'post_status' => 'publish',

'posts_per_page' => 12,

'caller_get_posts'=> 1,
'tax_query' => array(
array(
'taxonomy' => 'book_author',
'field' => 'id',
'terms' => get_queried_object_id()
)
),

'orderby'=> 'date',

'order' => 'DESC'

);

$my_query = null;

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

echo '';

$i = 0;

while ($my_query->have_posts()) : $my_query->the_post();

if($i % 6 == 0) { ?>

<div class="row">

<?php

}

?>



<img src="<?php the_field('cover_image'); ?>" /><br />

<?php the_field('author_name'); ?><br />

<?php the_title(); ?><br />



</div>



<?php

if($i % 6 == 0) { ?>



<?php

}



$i++;

endwhile;

}

wp_reset_query();

?>


lowercase comments:

The AUTHORS are being defined with Wordpress Categories if that helps.
Can you modify your code to use categories instead of taxonomies?


Remy comments:

It´s even simpler, just add 'cat' => get_queried_object_id() to your query args and it should work


lowercase comments:

where?


lowercase comments:

where?


lowercase comments:

No authors are Not a custom taxonomy. Just regular wordpress categories.


Remy comments:


$args=array(

'post_type' => 'book',

'post_status' => 'publish',

'cat' => get_queried_object_id(),

'posts_per_page' => 12,

'caller_get_posts'=> 1,

'orderby'=> 'date',

'order' => 'DESC'

);


lowercase comments:

Thanks Remy, but unfortunately I get no results with this....

<?php
$args=array(
'post_type' => 'artist',
'post_status' => 'publish',
'cat' => get_queried_object_id(),
'posts_per_page' => 12,
'caller_get_posts'=> 1,
'orderby'=> 'date',
'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '';
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
if($i % 6 == 0) { ?>
<div class="row">
<?php
}
?>

<img src="<?php the_field('cover_image'); ?>" /><br />
<?php the_field('author_name'); ?><br />
<?php the_title(); ?><br />

</div>
<?php
if($i % 6 == 0) { ?>
<?php
}
$i++;
endwhile;
}
wp_reset_query();
?>

2014-02-09

Agus Setiawan answers:

modif with somthin like this ( please check on bold code )

<?php

$args=array(

'post_type' => 'book',

'post_status' => 'publish',

'posts_per_page' => 12,

'caller_get_posts'=> 1,

'orderby'=> 'date',

'order' => 'DESC'

);

$my_query = null;

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

echo '';

$i = 0;

while ($my_query->have_posts()) : $my_query->the_post();

if($i % 6 == 0) { ?>

<div class="row">

<?php

}

?>

<strong><?
if(the_field('author_name'=='Stephen KING') { ?>


<img src="<?php the_field('cover_image'); ?>" /><br />

<?php the_field('author_name'); ?><br />

<?php the_title(); ?><br />

<? } ?></strong>



</div>



<?php

if($i % 6 == 0) { ?>



<?php

}



$i++;

endwhile;

}

wp_reset_query();

?>


lowercase comments:

Can I do that dynamically? I have hundreds of authors so can't create hundreds of pages to input their specific name.
All authors are listed as Wordpress Categories if that helps.


Agus Setiawan comments:

yes of course,

please let me know your site ( by pm please ), i need to know something.


Agus Setiawan comments:

check on bold area

<?php

$args=array(

'post_type' => 'book',

'post_status' => 'publish',

'posts_per_page' => 12,

'caller_get_posts'=> 1,

'orderby'=> 'date',

'order' => 'DESC'

);

$my_query = null;

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

echo '';

$i = 0;

while ($my_query->have_posts()) : $my_query->the_post();

if($i % 6 == 0) { ?>

<div class="row">

<?php

}

?>

<strong><?

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;

if(the_field('author_name'==$category_id) { ?>


<img src="<?php the_field('cover_image'); ?>" /><br />

<?php the_field('author_name'); ?><br />

<?php the_title(); ?><br />

<? } ?>
</strong>


</div>



<?php

if($i % 6 == 0) { ?>



<?php

}



$i++;

endwhile;

}

wp_reset_query();

?>


lowercase comments:

that code throws an error. unexpected '{'.


Agus Setiawan comments:

hehe sorry, i missed something, change to this :

<?php

$args=array(

'post_type' => 'book',

'post_status' => 'publish',

'posts_per_page' => 12,

'caller_get_posts'=> 1,

'orderby'=> 'date',

'order' => 'DESC'

);

$my_query = null;

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

echo '';

$i = 0;

while ($my_query->have_posts()) : $my_query->the_post();

if($i % 6 == 0) { ?>

<div class="row">

<?php

}

?>



<?

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;

if(the_field('author_name'==$category_id)) { ?>


<img src="<?php the_field('cover_image'); ?>" /><br />

<?php the_field('author_name'); ?><br />

<?php the_title(); ?><br />

<?

}

?>



</div>



<?php

if($i % 6 == 0) { ?>



<?php

}



$i++;

endwhile;

}

wp_reset_query();

?>


lowercase comments:

Code no longer breaks but it doesn't solve the problem.
Can you suggest a better way I can do this perhaps?


Agus Setiawan comments:

hmmm, may i know your site please. ( send me by private message, so no one knows )
[email protected]


lowercase comments:

I can't do that for security reasons. What other information can I provide?


Agus Setiawan comments:

only site url. i am not asking you about user and password.


Agus Setiawan comments:

how about this :

<?

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;

if(the_field('author_name'==$categories) { ?>


<img src="<?php the_field('cover_image'); ?>" /><br />

<?php the_field('author_name'); ?><br />

<?php the_title(); ?><br />

<? } ?>

2014-02-09

Bob answers:

It means you are adding author as wordpress category and cusotm post type "book author" right?

if will be nice if you post your code about how you creating custom post types of book and book author.

or you can send me files(only which are creating post types) at [email protected]

I will send you full working code back.


Bob comments:

if you can paste code of custom post type here then I can help you.

I am sure It will not take more then an hour to figure out your problem.


lowercase comments:

Custom post types here...

Release:
add_action('init', 'cptui_register_my_cpt_release');
function cptui_register_my_cpt_release() {
register_post_type('release', array(
'label' => 'Releases',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'releases', 'with_front' => 1),
'query_var' => true,
'has_archive' => true,
'exclude_from_search' => true,
'supports' => array('title','editor','custom-fields','thumbnail'),
'labels' => array (
'name' => 'Releases',
'singular_name' => 'Release',
'menu_name' => 'Releases',
'add_new' => 'Add Release',
'add_new_item' => 'Add New Release',
'edit' => 'Edit',
'edit_item' => 'Edit Release',
'new_item' => 'New Release',
'view' => 'View Release',
'view_item' => 'View Release',
'search_items' => 'Search Releases',
'not_found' => 'No Releases Found',
'not_found_in_trash' => 'No Releases Found in Trash',
'parent' => 'Parent Release',
)
) ); }


Artist:
add_action('init', 'cptui_register_my_cpt_artist');
function cptui_register_my_cpt_artist() {
register_post_type('artist', array(
'label' => 'Artists',
'description' => 'Artists',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'artists', 'with_front' => 1),
'query_var' => true,
'has_archive' => true,
'supports' => array('title','editor','custom-fields','thumbnail'),
'labels' => array (
'name' => 'Artists',
'singular_name' => 'Artist',
'menu_name' => 'Artists',
'add_new' => 'Add Artist',
'add_new_item' => 'Add New Artist',
'edit' => 'Edit',
'edit_item' => 'Edit Artist',
'new_item' => 'New Artist',
'view' => 'View Artist',
'view_item' => 'View Artist',
'search_items' => 'Search Artists',
'not_found' => 'No Artists Found',
'not_found_in_trash' => 'No Artists Found in Trash',
'parent' => 'Parent Artist',
)
) ); }


Bob comments:

You said
<blockquote>No authors are Not a custom taxonomy. Just regular wordpress categories.</blockquote>

but your above code do not deal with "wordpress categories".

There should be something which is common medium between two post type.


lowercase comments:

I have now added a custom field within the RELEASE post type called 'link_to_artist'.
This allows me now to link all of the releases with an artist via a drop-down menu.

SO now every RELEASE has a custom field which returns a value for the artist via the field 'link_to_artist'.

Does this help?

2014-02-09

Deepak answers:

Try this one?

<?php

$args=array(

'post_type' => 'book',

'post_status' => 'publish',

'posts_per_page' => 12,

'caller_get_posts'=> 1,

'orderby'=> 'date',

'order' => 'DESC'

);

$my_query = null;

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

echo '';

$i = 0;

while ($my_query->have_posts()) : $my_query->the_post();

if($i % 6 == 0) { ?>

<div class="row">

<?php

}

?>

<?
if(the_field('author_name'=='Stephen KING'))
{ ?>
<img src="<?php the_field('cover_image'); ?>" /><br />
<?php the_field('author_name'); ?><br />
<?php the_title(); ?><br />
<? } ?>
</div>
<?php
if($i % 6 == 0)
{ ?>
<?php
}
$i++;
endwhile;
}
wp_reset_query();
?>

or try this?

<?php

$args=array(

'post_type' => 'book',

'post_status' => 'publish',

'posts_per_page' => 12,

'caller_get_posts'=> 1,

'orderby'=> 'date',

'order' => 'DESC'

);

$my_query = null;

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

echo '';

$i = 0;

while ($my_query->have_posts()) : $my_query->the_post();

if($i % 6 == 0) { ?>

<div class="row">

<?php

}

?>

<?
if(the_field('author_name')=='Stephen KING')
{ ?>
<img src="<?php the_field('cover_image'); ?>" /><br />
<?php the_field('author_name'); ?><br />
<?php the_title(); ?><br />
<? } ?>
</div>
<?php
if($i % 6 == 0)
{ ?>
<?php
}
$i++;
endwhile;
}
wp_reset_query();
?>


lowercase comments:

Thanks for the reply.

We can't use...

if(the_field('author_name'=='Stephen KING'))

because we have 100's of authors.
That would mean making a new template with a new query for EVERY artist?

2014-02-10

Hariprasad Vijayan answers:

Hello,

You mention you have two post types BOOK and BOOK AUTHOR. And i think you separately created category to mention "AUTHOR" while creating a post. It seems there is no relation between book and author because both are different post types. And i assume your author page is single page of custom post type "BOOK AUTHOR" and it can't query from that page.

Let me know my assumption is right or wrong. I can help you if i you provide more details.

Thanks.


lowercase comments:

That is right. You can see about my actual custom post type code above.
I think you make a good point.

I have now added a custom field within the RELEASE post type called 'link_to_artist'.
This allows me now to link all of the releases with an artist via a drop-down menu.

SO now every RELEASE has a custom field which returns a value for the artist via the field 'link_to_artist'.

Does this help?

Note - book/book author is actually relase/artist.


Hariprasad Vijayan comments:

Hello,

I would like to check dashboard. Can you provide credential? Please PM.


Hariprasad Vijayan comments:

I am not clear about it. My suggestion is

If you you are using two post types BOOK and BOOK AUTHOR i think there is no relation between them. So add a custom field in BOOK to pick book author. Then you can filter based on that value in BOOK AUTHOR page to display all books using meta_query.

OR

You said that you are using category as Author. I think you are selecting one category(Author) while adding a Book(Post in post_type BOOK). Then make the slug of Category and Book Author(Post in post_type BOOK AUTHOR) same. Then you can do something with it.

Does that make sense?

I am not clear about RELEASE and artist etc.. Thats why am explaining like this. Let me know if you need any help