Forums

[resolved] how to get/query posts given a post_content (2 posts)

  1. prettynerd
    Member
    Posted 2 years ago #

    i would like to retrieve a post (an attachment, to be particular) with a description equal to my choosing..

    i have this code:

    query_posts('post_type=attachment&meta_key=post_content&meta_value=customAdLeft');

    but it's not working.. i do think this is invalid code.. =)

    please help.. thanks!

  2. prettynerd
    Member
    Posted 2 years ago #

    i found the solution myself! ehe..

    <?php

    $keyword = "keyword";
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => null, // any parent
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $post) {
    if(trim($post->post_content)==$keyword){
    setup_postdata($post);
    the_attachment_link($post->ID, false);
    }
    }
    }

    ?>

    if someone can help me make this shorter, please do.. =)

Topic Closed

This topic has been closed to new replies.

About this Topic