Wordpress query_posts with ID and slug combination - wordpress

is it possible to use query_posts with a combination of id's and slugs?
I have an array of id's and slugs from user input and want to know if I can use the two in the same post query, or do I have to convert the slugs into their respective post ID's before and then use posts__in?
I have the following mixture of slugs and ID's in an array…
$values = array('this-is-a-test-slug', '2345', '4510', 'another-slug-here', '8934');
How can I use this in query_posts? Can I at all?
query_posts(array('post_type' => 'post', 'post__in' => $values, 'orderby' => 'rand'));
I know that post__in works ok with numeric ID's but I don't think slugs work here as it expects a numerical array.
Thanks

If you're doing something like this, i don't see why it wouldn't be a problem to just convert them all over to ID? There's a thread that sort of helps, and I've written some code (with help from that link) that might help you get started
function allToID($array){
$id = array();
foreach($array as $convert):
if(is_numeric($convert)):
continue; // skip if it's already an ID
else:
$the_slug = $convert;
$args=array(
'name' => $the_slug,
'numberposts' => 1
);
// Ask wordpress to get this post
$my_posts = get_posts($args);
if( $my_posts ) :
// push onto our new array of only IDs
array_push($id, $my_posts[0]->ID);
else continue;
endif;
endif;
endforeach;
}
Ideally you'll be able to run post__in => alltoID($values)
Hope this helps!

Related

Accessing Advanced Custom Fields by Page Name

I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.
The are too ways to do this that come to mind...
1. Using the Loop
Using WP_Query you can do something like this...
<?php
// WP_Query arguments
$args = array (
'pagename' => 'homepage',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_field( "field_name" );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
In place of the 'homepage' in 'pagename' => 'homepage', you want to put the page slug of your page. And of course in place of the_field( "text_field" ); you want to add your fields/content.
You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF's built-in the_field() function with the $post->ID parameter added as a second parameter.
<?php the_field('field_name', 123);
This might be the way to go since you want to show the content of only one page, and therefore don't really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
You can use ACF's get_fields() function -
<?php $fields = get_fields( $post->ID ); ?>
You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/

Wordpress: retrieve first five items from all populated categories

I am trying to find a way to achive the following: I'd like to make a custom template. Within this template I'd like to list the name of each category on my site that has at least 1 item assigned to it. Beneath the category name (and link) I'd like to insert some content from the first x number of items that have that particular category assigned.
Just in case it makes a massive difference the items in question are not posts, but custom items.
Can anyone give me some pointers or help? I assume there will be something I can do with the wp_query function, but I'm not really sure how to inject it between each of the category titles, or indeed how to make it work with a category for which I can't explicitly provide an id in the code).
Thank you.
get_posts() is your ideal solution. For example:
<?php $posts_array = get_posts( $args ); ?>
<?php $args = array(
'posts_per_page' => 1,
'category' => $post_cat,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'yourcustompostname',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
///respective loop here ie : foreach $myposts as $mypost ... etc
?>
Where $post_cat is the id of your category and post_type is the custom post type.
To keep these 'items' organized I would create a custom post and then assign a category to those posts, for each respective category (if there are multiple). If there is just one, than you could live with just using a single category.
EDIT:
to get the categories assigned to this custom post take a look at this link
Using the results from this you could assign the cat id's to the variable created earlier: $post_cat.
You have to use query_posts function
query_posts(array('category_name'=>'category-slug','posts_per_page'=>'5'));
then you need to do
while ( have_posts() ) : the_post();
and you can get the post id using:
$p_id = get_the_ID();
for more information : query_posts
You can use get_categories()
http://codex.wordpress.org/Function_Reference/get_categories
But, then you need to find out where the data for 'items' is stored, and query depending on that.
Here is the answer of your problem. You need to change the category id of own post category id .
Here is the code. just copy and past it.
$query1 = new WP_Query( array( 'post_type' => 'post','cat' =>'10,9') );
Use the above query in loop and after loop,reset your query . see the below code.
wp_reset_query();
Please use it and let me know if needs anything else.

how do i enter a var in a wordpress custom query?

how do I put a custom var in the following line of the below
AND $wpdb->postmeta.meta_value = 'email'
I want to put in a var for 'email'
something like
AND $wpdb->postmeta.meta_value = $var
anyone know how to do this is Wordpress. I think i need to bind?
re: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
It depends a bit on context, but if you really are performing a custom query (as, say, part of a widget) you would provide it as an argument to WP_Query:
$query = new WP_Query( array( 'meta_value' => 'user#example.com' ) );
Oftentimes this would include a corresponding meta_key in that argument array.
If you want to modify the currently running query (say, on a category page, only show posts that match a certain criteria) you would perform that modification during the pre_get_posts action. Recommended reading: Andrew Nacin's You Don't Know Query.
I had done that using custom query plugin Go here
else custom your query like this
<?php
$args = array('post_type' => 'page','meta_query' => array(array('key' => 'email','value' => 'yes','compare' => '%')));
$var = new WP_Query($args);
// The Loop
while ( $var->have_posts() ) : $var->the_post();
$P_ID = get_the_ID();
endwhile;
// Reset Post Data
wp_reset_postdata();
Hope it helps

Search for Custom Field with Custom Post Types

There are tutorials that explain how to limit a search to a specific category.
My question is, is there a way to configure wordpress' search to, within a custom post type, search for a custom field value.
So for example, if I search for "hello", the results would come up with posts that have a certain custom field equal to "hello". The certain post would also be a certain custom post type.
Any help is appreciated.
To filter search by custom post type use:
<?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
before the loop.. then within the loop add a condition similar to this
<?php if ($meta_data[ 'meta-name' ] == 'hello') {
//do something
} ?>
I think here is what you are looking for:
key is the custom field.
value is the value you are looking for
and compare is the operator you want to use.
you can also use LIKE if you want.
// WP_Query arguments
$args = array (
'post_type' => 'vendors',
'post_status' => 'published',
'meta_query' => array(
array(
'key' => 'state',
'value' => 'Misissipi',
'compare' => '=',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();

Wordpress - multiple WP Query objects into one?

In Wordpress it's possible to create own WP Querys for the loop. An example is this:
$my_query = new WP_Query(array('post_parent' => 3, 'post_type' => 'page'));
Another example is this:
$my_query = new WP_Query(array('cat' => 1, 'post_type' => 'post'));
I want a loop that presents pages AND posts from the same loop.
Now to my question. Is it possible to combine these two objects into one? If it is, how? I'm NOT interested in creating two different loops.
In case you don't want to do it with SQL, this is how I did my search page.
Basic problem: When doing a meta_query, wordpress thinks I want the condition to be joined with "AND" instead of "OR".
So Wordpress looks for a page with title/content = "myContent" AND the aioseop_keyword "myContent". This (in my case) lead to zero results, despite there was a page with matching SEO keyword.
To get around this, I make two queries. Sounds simple, BUT: the Loop didn't want to recognize the posts, despite there are posts in the $post object. I found this solution after taking a look on the have_posts() function : it refers to other variables than just the $post object.
$term = get_search_query(); // same as $_GET['s']
# the normal search:
$wordpress_keyword_search =& new WP_Query(array(
's' => $term,
'showposts' => -1
));
# now push already found post IDs to an array, so we can exclude them from the meta search.
foreach ($wordpress_keyword_search->posts as $post_)
$exclusion[] = $post_->ID;
# now do the meta query search
$aioseop_keyword_search =& new WP_Query(array(
'post__not_in' => $exclusion,
'post_type' => 'any',
'showposts' => -1,
'meta_query' => array(
array(
'key' => '_aioseop_keywords',
'value' => $term,
'compare' => 'LIKE',
)
)
));
# merge the two array posts.
# post_count and found_posts must be added together also.
# otherwise have_posts() returns false.
# see: http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/query.php#L2886
$wordpress_keyword_search->posts = array_merge($wordpress_keyword_search->posts, $aioseop_keyword_search->posts );
$wordpress_keyword_search->found_posts = $wordpress_keyword_search->found_posts + $aioseop_keyword_search->found_posts;
$wordpress_keyword_search->post_count = $wordpress_keyword_search->post_count + $aioseop_keyword_search->post_count;
Then use this in a simple loop:
if ($wordpress_keyword_search->have_posts()) {
while($wordpress_keyword_search->have_posts()) {
$wordpress_keyword_search->the_post();
# now you simply can:
the_title();
the_content();
}
} else {
echo '<p>Sorry, no posts found</p>';
}
what you want would translate to a WHERE ... OR ... condition or a UNION in SQL, eg.
SELECT * FROM posts WHERE (post_parent = 3 AND post_type = 'page')
OR (cat = 1 AND post_type = 'post')
or
SELECT * FROM posts WHERE post_parent = 3 AND post_type = 'page'
UNION
SELECT * FROM posts WHERE cat = 1 AND post_type = 'post'
from looking at the source and the way WP constructs SQL from WP_Query(), i don't think this is possible: there is no OR'ing nor UNION of query vars.
the only thing that comes to my mind is writing a plugin that implements the posts_where filter (applied to the WHERE clause of the query that returns the post array). you could call this plugin with your different WP Querys, and the plugin would get their WHERE parts and could OR them together.
see also http://codex.wordpress.org/Custom_Queries

Resources