• I tried this but it does not work. Can someone advise on the syntax?

    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts' => array( 'post_excerpt' => 'ba' )) );

Viewing 10 replies - 1 through 10 (of 10 total)
  • Please describe in more detail what you are trying to do, with examples.

    Thread Starter sandraqu

    (@sandraqu)

    Hi. I have a portfolio, and wish to show only posts from the “portfolio” with a certain “skill” (custom instead of category), on a page.

    this loop is getting called from a page template, and i cannot see the “skill” in the array, thus I added a code in the excerpt field, and am trying to target that field in the array to build an array with only items that have ‘ba’ in the excerpt field.

    Is that helpful?

    Better, but still not quite clear. Is the skill in a Custom Field? If so, what is the field name? If not, why not?

    Thread Starter sandraqu

    (@sandraqu)

    I’m a newbie and am working with a theme that was created by someone who has an good understanding of WP. I am trying to apply my limited skills to the scripts. With your help I hope to get this right.

    I don’t know where the custom field lives. I looked at the database, and it seems that there is a post_parent that has the same numbers for a series of posts, and I hope that corresponds to the “Skill” custom field.

    An excerpt of the WP Query array follows. My attempt at filtering is not working:

    [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'portfolio' AND (
    wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 10 [posts] =>

    the array excerpt:

    WP_Query Object (
     [query_vars] => Array (
     [post_type] => portfolio [paged] => 1 [posts[0]] => Array (
     [post_excerpt] => ba ) [error] => [m] => 0 [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [category__in] => Array (
     ) [category__not_in] => Array (
     ) [category__and] => Array (
     ) [post__in] => Array (
     ) [post__not_in] => Array (
     ) [tag__in] => Array (
     ) [tag__not_in] => Array (
     ) [tag__and] => Array (
     ) [tag_slug__in] => Array (
     ) [tag_slug__and] => Array (
     ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [posts_per_page] => 10 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object (
     [queries] => Array (
     ) [relation] => AND ) [meta_query] => WP_Meta_Query Object (
     [queries] => Array (
     ) [relation] => ) [post_count] => 10 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 96 [max_num_pages] => 10 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => 1 [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash] => d93ccb2ea312a6d1 [query_vars_changed] => [thumbnails_cached] => [query] => Array (
    
     [post_type] => portfolio [paged] => 1 [posts[0]] => Array (
     [post_excerpt] => ba ) ) [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'portfolio' AND (
    wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 10 [posts] => Array (
     [0] => stdClass Object (
     [ID] => 696 [post_author] => 1 [post_date] => 2012-05-01 18:51:28 [post_date_gmt] => 2012-05-01 18:51:28 [post_content] => [post_title] => White Pearl, Rock [post_excerpt] => ba [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => white-pearl-rock [to_ping] => [pinged] => [post_modified] => 2012-05-01 18:51:28 [post_modified_gmt] => 2012-05-01 18:51:28 [post_content_filtered] => [post_parent] => 0 [guid] => http://mydomain.com/dev/?post_type=portfolio&p=696 [menu_order] => 0 [post_type] => portfolio [post_mime_type] => [comment_count] => 0 [filter] => raw ) [1] => stdClass Object (
     [ID] => 723 [post_author] => 1 [post_date] => 2012-05-01 17:12:15 [post_date_gmt] => 2012-05-01 17:12:15 [post_content] => New floor and steps, State, CA. [post_title] =>

    Per forum rules, you may only post 10 lines of code here.

    Please use a pastebin for code over 10 lines long.

    A Custom Field is defined on the panel where the Post is created/edited. CF’s are stored in the postmeta table.

    If all you want to do is select all posts from the post type ‘portfolio’ that have a CF named ‘skill’ with a value of ‘flycatching’, then your query would look like this:

    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'meta_key' => 'skill', 'meta_value' => 'flycatching' );
    Thread Starter sandraqu

    (@sandraqu)

    Thanks for the pastebin tip. My language was inaccurate. The ‘skill’ is not a Custom Field. It’s customized, but not a Custom Field.

    Furthermore, WP_Query seems to target one table: wp_post. I need to target the wp_post table, and a wp_term table

    So I need to pass post_type= ‘portfolio’ and term_taxonomy_id = 11 and call two tables.

    wp_post.post_type = portfolio and give me only those where wp_term.term_taxonomy_id = wp_post.ID.

    For some reason, trying for post_excerpt = “ba” or even post_author = “3” will not filter. I don’t understand why. I tried isolating the posts I need to one author to see if a filter of that type would work.

    Please take a step back and describe the problem, not what won’t work.

    The Codex lists all of the available parameters to WP_Query.

    Thread Starter sandraqu

    (@sandraqu)

    I need to filter post by post_excerpt contents. When I try using WP_Query as follows, I get all posts as a result in the resulting array. How can build an array that only contains post with post_excerpt = ‘this’ ?

    This does not work:

    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts' => array( 'post_excerpt' => 'this' )) );

    and this does not work:

    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'post_excerpt' => 'this' ) );

    ‘post_excerpt’ is not a valid parameter to WP_Query. See the Codex article I cited above for the valid parameters.

    Why do you need to filter by post_excerpt contents? That is not an easy thing to do. Can you use a Custom Field instead?

    Please tell me what you are trying to accomplish, not how you are trying to solve the problem.

    I think you might need Taxonomy Parameters and the post type?

    HTH

    David

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to use WP_Query to filter by post excerpt’ is closed to new replies.