WordPress.org

Ready to get started?Download WordPress

Forums

filter pages by custom field (13 posts)

  1. public_radio
    Member
    Posted 7 years ago #

    Hello,

    I'm feeding a project I'm working on into WordPress. Some of the pages belong in the top nav, some in the subnav under that and some pages link from the footer.

    I would like to set up three loops for these pages, and filter by a custom field that I named nav-level with the possible values topnav, subnav and footnav.

    First, is this the best way to do this? I need to reserve my categories for other content that does not pertain to specific pages, like my news scroller and some other things.

    Second, how to I do this? I saw this post:
    http://wordpress.org/support/topic/67543?replies=3

    which seems to kind of do what I want, only for posts, not pages. What should I do?

    Thanks in advance.

  2. Kafkaesqui

    Posted 7 years ago #

    I won't comment on your 'first.' But if someone has some input on it, I'm sure they'll step in.

    On your 'second,' the SQL version of the code from the thread you linked to can be modified for this:

    <?php
    $customkey = ''; // set to your custom key
    $customvalue = ''; // set to custom value

    global $wpdb;
    $my_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->postmeta WHERE ID = post_id AND meta_key = '$customkey' AND meta_value = '$customvalue' ORDER BY post_date DESC");

    foreach($my_posts as $post) :
    ?>

    ~ "Loop" template tags and whatnot go here ~

    <?php endforeach; ?>

    With no category requirement you have just the $customkey and $customvalue to assign. It's likely you'll want to play with the order of these Pages, changing:

    ORDER BY post_date DESC

    to something like:

    ORDER BY post_title ASC

    For future reference, this can be used with either posts or Pages (or both!), since you only need to match custom field key=value.

  3. public_radio
    Member
    Posted 7 years ago #

    ok, so all this:

    foreach($my_posts as $post) :

    I can leave the word posts in there and it knows i'm talking about pages?

  4. public_radio
    Member
    Posted 7 years ago #

    ok I followed your advice and I think it worked, almost.

    sandwiched inside this code i have this:

    <?php wp_list_pages(); ?>

    which creates a loop within a loop, which is obviously wrong. However, it's spitting out this loop 7 times which is consistant with the number of pages with the key I'm calling.

    Here's another wrinkle: there are subpages under these pages. so the parent pages have a key and value and then those have child pages. I was hoping i could do something like <?php wp_list_pages('nav-level=topnav'); ?> but I guess I can't. Suggestions? Thanks for responding.

  5. Kafkaesqui

    Posted 7 years ago #

    "I can leave the word posts in there and it knows i'm talking about pages?"

    Yes. $post is a default object in WordPress which defines any single post or Page, so consider it a generalized name for the variable holding each Page within the foreach loop.

    As for wp_list_pages(), the code block I provide above effectively replaces it in your templates. They cannot be combined, as you probably noticed...

    If you're looking for a way to tie in your custom field setup with wp_list_pages(), I can't suggest an option for you here. As for replacing it with something which covers both the key=value Pages and their children, you'd have to first query the initial custom field Pages (as above), then match their ID's with the child Pages, making sure to array them with the individual parent Pages. Hmm.

    Can't provide a guarantee, but I'll do some thinking on this...

  6. public_radio
    Member
    Posted 7 years ago #

    This is a big help, Kafkaesqui. Everything is working like a charm, except for the child pages.

  7. public_radio
    Member
    Posted 7 years ago #

    ok, so i got the subpages to work too, by putting this in the loop:

    http://codex.wordpress.org/Template_Tags/wp_list_pages#List_Child_Pages


    <?php if(wp_list_pages("child_of=".$post->ID."&echo=0")) { ?>
    <ul>
    <?php wp_list_pages("title_li=&child_of=".$post->ID."&sort_column=menu_order&show_date=modified&date_format=$date_format");?>
    </ul>
    <?php } ?>

    Thanks again for your help.

  8. Kafkaesqui

    Posted 7 years ago #

    Obviously I was *way* overthinking what you were asking for. Glad you found a solution.

  9. Paracetamol
    Member
    Posted 7 years ago #

    Trying to sort posts via custom field tags, this post helped me a lot: I managed to display and sort posts with a certain key, in this case "Year", which I allow the user to enter, if necessary.

    But there's still something missing: 1) I can't get results only for the single active category and 2) can't sort them out by pages, they 3) appear as one list, leaving out the untagged ones.

    My Loop Template (derived from this topic):

    <?php $pageposts = $wpdb->get_results("SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'Year'
    ORDER BY wpostmeta.meta_value DESC", OBJECT); ?>
    <?php if ($pageposts) : foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
    //The Loop
    <?php endforeach;?>
    <?php endif; ?>

    Any help with the mysteries of SQL queries would be greatly appreciated.

  10. thedesigncoalition
    Member
    Posted 7 years ago #

    I am doing the same thing and have this same problem:

    " 1) I can't get results only for the single active category"

    It displays archives from all categories and not just the respective category.

  11. Kafkaesqui

    Posted 7 years ago #

    "It displays archives from all categories and not just the respective category."

    Psst: http://wordpress.org/support/topic/59676?#post-466302

    (Just keeping things tied together...)

  12. tomazpipan
    Member
    Posted 7 years ago #

    Hy Guys!

    I have a similar problem, but I would like to show things with wp_list_pages() tag listed by a META tag.

    LINK

    If anyone can help I would apprichiate!

    Thank you!

  13. kerfyl
    Member
    Posted 6 years ago #

    Hi, I'm picking up after:

    sandwiched inside this code i have this:

    <?php wp_list_pages(); ?>

    which creates a loop within a loop, which is obviously wrong. However, it's spitting out this loop 7 times which is consistant with the number of pages with the key I'm calling.

    I get the same result as public_radio when I use 'wp_list_pages()' - it displays all of the pages Twice, which is how many Pages have the custom key/value pair I am querying (i.e. 2 Pages)

    Basically, I'm trying the same thing with a leftnav, rightnav for the Pages - I want some of the Page links to be in the Sidebar, some of the Page links to be in the header.

    I tried doing the following:
    foreach($my_posts as $post) :
    echo $my_posts[1];
    endforeach;
    Basically, this was just a test to see what $my_posts[1] returned. But, I received the following error:

    Object of class stdClass could not be converted to string

    I looked it up, and it appears to be an issue with PHP 5.2; you can't echo out the array's data.

    How can I pull out the data from this $my_posts array? Somehow I need to convert the data from $my_posts to a string... any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic