Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to create a custom query loop to display data depending on the current post title.

Pseudo-code:

if post title = custom filed 
    Show related content

I have a page called artists made up of custom fields and I would like to show the discography for that artist. By pulling only the posts with the artists name from the discography posts which are in another custom post type.

I found this page which is close but not quite right.

I have a custom post type called discography, and within this post type I have a custom field called dis_artist.

I am stuck being able to say if the current artist "post title" = "dis_artist" then show other data. How can I do that?


Hi I tried but Im not sure I have the right syntax. This piece of code sits within the current post loop

<?php
$args = array( 'post_type' => 'discography', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>            
<?php
if ( get_the_title() === get_post_meta( get_the_ID(), "dis_artist", true ) ) {   ?>
<?php the_title(); ?>
<?php }    ?>        
<?php endwhile; ?>
share|improve this question

3 Answers 3

Are you using advanced custom fields or not? They have their own built in functions that look for the custom field.

if (get_field('dis_artist'))
{
   //do stuff here
   // the_field('dis_artist') is the same as $dis_artist = dis_artist; echo $dis_artist
}

Your title is slightly confusing.

If you set the ACF field to a variable, you can then test it inside your loop:

 if (get_field('your_acf_field')
 {
    $your_acf_field = get_field('your_acf_field');
 }


$args = array( 'post_type' => 'discography', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

if ($your_acf_field == the_title()) : echo the_title();

else :

 //do something else here if it is not

endif;

<?php endwhile; ?>

This code isn't tested, so you may need to tweak it. But it tests if the acf field is set, if it is, it sets it to a variable. Inside of the loop, you then test to see if the title is equal to the variable. If it is, it displays it. If not, you can have it do something else, like echo "Not Found";

share|improve this answer
    
Yes I am using ACF. I added the code here but. Im really stuck! wordpress.stackexchange.com/questions/67043/… –  joachim Oct 3 '12 at 23:17

Hi I tried but Im not sure I have the right syntax. This piece of code sits within the current post loop

<!-- Start the main loop -->
<?php the_title(); ?>

I want to use this title as the main reference to query against the custom filed

<?php
$args = array( 'post_type' => 'discography', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>            
<?php
if ( get_the_title() === get_post_meta( get_the_ID(), "dis_artist", true ) ) {   ?>
<?php the_title(); ?>
<?php }    ?>        
<?php endwhile; ?>

<?php endwhile; ?> <!-- End the main loop -->
share|improve this answer

Should be as easy as this - as long as your dis_artist meta key doesn't hold an array.

if ( get_the_title() === get_post_meta( get_the_ID(), "dis_artist", true ) )
{
    // do stuff
}
share|improve this answer
    
Ok I will try this. Thanks for the super fast responce –  joachim Oct 3 '12 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.