Wordpress - Custom fields & Class Identifiers - css

In Wordpress - how do you assign a class to custom fields? I am attempting to target multiple custom fields with custom CSS, but seem unable to give them separate classes.

This is probably not the BEST solution, but is worth a shot, I'd do a loop per field so your get_posts would look like this:
$args = array(
'meta_key' => 'custom_attribute',
'meta_value' => 'value1',
);
$value1 = get_posts($args);
$args = array(
'meta_key' => 'custom_attribute',
'meta_value' => 'value2',
);
$value2 = get_posts($args);
So that will give you 2 arrays, each one pulling different posts, from that you can do a foreach going through each array (value1 and value2) and adding classes as needed.

Thanks for your help.
I've since discovered a great bit of syntax that accomplishes the job.
<?php echo '<li class="address">' . get_post_meta($post->ID, 'Address', true) . '</li>'; ?>
This pulls in one single custom field at a time. In the example above, my custom field title was 'Address'. The will wrap the content from that field - thus accomplishing the task of wrapping each custom field in it's own class.

Related

Using Customizer to define category_name in Wordpress

I am trying to use the Wordpress Customizer to allow the client to input the category names for post that will appear on the homepage. The design calls for two columns of posts, with definable categories for each column.
My hope is that I could do something like:
if( get_theme_mod( 'column_1_category') != "" );
$args = array(
'category_name' => echo get_theme_mod( 'column_1_category'); ),
'posts_per_page' => 2
);
I have already defined the column_1_category in customizer.php, and it works great by itself, but I would like whatever category is typed into the customizer to define the category_name in the $args = array( code.
The 'category_name' = > echo . . is the line that keeps giving me errors. I am assume, if this even works, that I am missing some code in there?
I was hoping this would be a quick and easy way to define the Category, but not sure if this would even work?
Thanks.
Nevermind, i figured it out. All I needed to do was:
$category_2_name = get_theme_mod( 'column_2_content');
$args = array(
'category_name' => ($category_2_name),
'posts_per_page' => 2
);

Wordpress show all Custom Post Type except one

I am using this loop:
<?php
$search_count = 0;
$search = new WP_Query("s=$s & showposts=-1");
if($search->have_posts()) : while($search->have_posts()) : $search->the_post();
$search_count++;
endwhile; endif;
echo $search_count;
?>
How can I say, that I want to show every Custom Post Type, except for the post type called 'klanten' (clients in Dutch)?
You can use like this:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => array( 'post', 'page', 'movie', 'book') // include the posts type you want to show
);
$query = new WP_Query( $args );
Unfortunately, there's currently no way you could exclude a specific post_typeby defining it. So, the workaround is you only define the post_type you want to include.
A couple of years too late, but you can use the get_post_types() function with the operator parameter set to not. This way, you will receive an array of all post types except the one(s) you set in the args parameter of the function, which you can then use to define the post_type in your query:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => get_post_types(array('name' => 'klanten', 'public' => false), 'names', 'not')
);
$query = new WP_Query( $args );
However, you might want to var_dump() the result of the function first, since it will return other hidden post types you might not be interested in (like 'attachments' or others defined by plugins). This is the reason why I added 'public' => false, which will make sure that I only receive public post types. This removes most of the undesired post types, but you should still check ('attachments' is not removed by this).
Another shortcoming of this method is that you can only remove one post type. A workaround for that would be to get all post types and then remove the ones you don't want from the array using the method described here, but that seems like too much code, so in that case you might be better off just setting all the post types you want instead of the ones you don't.

Wordpress query_posts with ID and slug combination

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!

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.

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();

Resources