• I’m using this code to display a loop within the main loop:

    <?php $my_query = new WP_Query('category_name=<strong>HERE-IS-WHERE-I-WANT-TO-INSERT-CUSTOM-FIELD</strong>&orderby=title&order=asc&showposts=100');while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate=$post->ID;?>
    
    <li><?php the_title();?></li>
     <?php endwhile; ?>

    How could I insert a custom field within that php code???
    I tried this:

    <?php
    $post_title = get_post_meta($post->ID, 'post_title', $single = true);
    ?>

    then inserting $post_title. within the code.

    Didn’t work. There must be a way?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter zefdesign

    (@zefdesign)

    I’m sure this is doable but I can’t find anything saying how…

    Thread Starter zefdesign

    (@zefdesign)

    PLease help monday people!!!!!

    I personally prefer to use this method..

    $my_query = new WP_Query();
    $my_query->query('orderby=title&order=asc&showposts=100');

    Preferences aside..

    All you need to do is add the meta parameters to the above query, it works the same as a regular query_posts, so just add what meta parameters you need.

    See.
    https://codex.www.ads-software.com/Template_Tags/query_posts#Custom_Field_Parameters

    Thread Starter zefdesign

    (@zefdesign)

    Thanks for responding, I am very bad at php however and still don’t know how to apply the code. I tried using it but it wasn’t working for me. the code I posted does work… but I don’t know hwo to stick my custom field in the code.

    Click the link i gave you above, the parameters are covered on that page, all you need to do is add them in with your other parameters..

    example..
    ('order=asc&meta_key=SOMEFIELNAME&meta_value=SOMEVALUE')

    The link provides ample examples.

    Consider $my_query->query(); as the equivalent of query_posts();

    So for example..
    query_posts('showposts=1');
    in the custom query would be..
    $my_query->query('showposts=1');

    Thread Starter zefdesign

    (@zefdesign)

    Maybe I didn’t explain what I mean clearly enough.

    I have a post for an album for example it’s called ‘Apples’. That post has a custom field called post title which I assing the vale ‘Apples’.

    I then have my code in single.php. And I want it to display a list of posts from the category name of ‘Apples’. But i want to assing the category name using my custom field.

    eg:

    <?php $my_query = new WP_Query(‘category_name=INSER CUSTOM FIELD HERE

    Ok, you mean something like this?

    <?php
    $fieldname = 'Apples';
    $field = get_post_meta( $post->ID , $fieldname , true );
    if(!$field) echo $fieldname . ' missing for this post' ; return;
    
    $my_query = new WP_Query();
    $my_query->query('category_name='.$field.'&orderby=title&order=asc&showposts=100');
    
    	while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate=$post->ID;
    	?>
    		<li><?php the_title();?></li>
    	<?php
    	endwhile;
    	?>
    Thread Starter zefdesign

    (@zefdesign)

    That looks exactly like the right sort of thing actually, thanks. I tried it and it hasn’t generated anything.

    The code I would normally use would be: <?php $values = get_post_custom_values("post_title"); echo $values[0]; ?> But obviously I can’t just put that in the code…

    Sorry about this, but i’m real grateful

    $field will be the value of the custom field…

    So the code i posted will look for a category with the name of whatever you placed in the custom field called “Apples” per the example..

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Insertin custom field in php code?’ is closed to new replies.