The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End how do i write this if statement?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #25473

    Hey guys hopefully someone out there can help me with this.

    I have a bunch of pages that have custom fields within them. One of the custom fields I created is a checkbox named "featured". I’d like to grab the custom field values from all the pages that checked off the checkbox and feature them in the sidebar.


    5, ‘post_parent’ => 90, ‘post_type’ => ‘page’));

    while (have_posts()){


    $thumbPath= get_post_meta($post->ID, ‘feature_thumbnail’ , true);
    $featureTitle = get_post_meta($post->ID, ‘feature_title’ , true);
    $featureDescription = get_post_meta($post->ID, ‘feature_description’ , true);


    Right now the code echo’s out the custom fields but the problem is i only want to echo out the custom fields from the pages with the featured checkbox clicked. Thanks :D


    an if statement in PHP is very easy,

    it looks like this:


    For example:

    if ($checked = true) {
    /*code to show post*/

    however I don’t know how you currently grab the checkbox’s value for each post yet, but that’s how to use an if statement.


    you’d probably want to do:

    if($checked == true) {
    /* code here */

    if you do $checked = true, you’re going to be setting $checked to true, not actually validating that the variable $checked already holds the value true.


    How do I write this if statement? You don’t! You don’t need an If statement here, you need to go back to your query_posts statement. Since you only want to select posts that have a certain custom field (or post meta in wordpress parlance) you will need add a couple of more parameters to this statement.

    This will depend of course on what you called your custom fields and exactly what checking the box sets the field value to (since custom fields can only contain text – it might be set to ‘1’, ‘true’ or ‘TRUE’ depending on what you used to add the checkbox).

    query_posts(array(‘showposts’ => 5, ‘post_parent’ => 90, ‘post_type’ => ‘page’,
    ‘post_status’ => ‘publish’, ‘meta_key’ => ‘featured’, ‘meta_value’ => ‘1’));

    In this example the custom field is called ‘featured’ and a checked off checkbox sets the field to ‘1’.

    Hope that is what you were trying to achieve – any problems let me know.

    PS It’s always wise to include post_status=publish, unless you want draft posts to be included the list.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.