WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to use custom object field value as parameter in query_posts (3 posts)

  1. Mango123
    Member
    Posted 1 year ago #

    Hi there,

    I've created a custom field which links to the page object of another page using the plugin advanced custom fields. I now want to use query_posts to return all of my posts that link to a particular page using that page object custom field.

    The value I would like to filter is found using:
    `$field = get_field('on_page'); // my custom field
    echo $field->post_title;`

    Can I use $field->post_title as a parameter in query_posts? If so is it possible to show me how I would do it?

    Many thanks,

    Ben

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Please explain what you want as the results of the query_posts()?

  3. Mango123
    Member
    Posted 1 year ago #

    Hi vtxyzzy,

    Thanks for your question, I wanted to filter query_posts so it would only return results based on a custom field value.

    I managed to find out that I could use the object ID of the custom field. So for 'meta_value' I could use the value found from $field->ID.

    The code I ended up using was:

    wp_reset_query();
    		$args = array(
    			'post_type' => 'feature',
    			'orderby' => 'date',
    			'order' => 'ASC',
    			'posts_per_page' => 4,
    			'meta_key' => 'on_page',
    			'meta_value' => 2
    		);
    		query_posts( $args );

    The meta_value I found by printing $field->ID.

Topic Closed

This topic has been closed to new replies.

About this Topic