• While I’ve not new to WordPress I’m very new to using PHP in any meaningful way. With that being said here’s my problem.

    I want to be able to use the value of a custom meta field that I’ve created, in this case the key “hubcategory”. Every time I attempt to do that I get a php error. I’m sure it’s something very basic that I’m missing but so far nothing. Any help you guys could provide would be awesome.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi harlemS,

    Can I get the code that you try to implement ?

    Please post ten or less lines of the code you are trying to use, starting 5 lines before where you are retrieving the CF, and including the line to get the CF.

    Must have the ID.. Try something like this:

    <?php $pageid = get_the_ID();
    echo get_post_meta($pageid, 'hubcategory', true); ?>

    Thread Starter harlemS

    (@harlems)

    I tried this:

    <?php query_posts(array('cat'=>get_post_meta($pageid, 'hubcategory', true))); ?> But i’m getting a Syntax error.

    If you are using this to get the category id stored in a custom field named ‘hubcategory’, on a Page, please put the code for the entire template in a pastebin so it can be examined.

    This may work, depending on the template:

    <?php $cat_id = get_post_meta($post->ID,'hubcategory',true);
    query_posts(array('cat' => $cat_id); ?>
    Thread Starter harlemS

    (@harlems)

    Here’s a paste bin of the code.

    You are using both a ‘new WP_Query()’ and a ‘query_posts()’ in your code. You only need the WP_Query. Try replacing this:

    <?php $wp_query = new WP_Query(array('showposts'=>12,'paged'=>$paged)); ?>
    <?php $cat_id = get_post_meta($post->ID,'hubcategory',true); query_posts(array('cat' => $cat_id); ?>

    with this:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $cat_id = get_post_meta($post->ID,'hubcategory',true); ?>
    <?php $wp_query = new WP_Query(array('showposts'=>12,'paged'=>$paged, 'cat' => $cat_id)); ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using a Custom Field value inside a post query’ is closed to new replies.