Does someone know how to get the first comment ID from the current post?
I have already searched long for a good solution, but found nothing suitable.
I appreciate any help.
get_comments() returns an object. So you can just access the first one like any other object:
$comments = get_comments();
$first_comment_id = $comments[0]->comment_ID;
You have to use the following function to get comments of a post
<?php get_comments( $defaults ); ?>
It's default usage is as follows:
<?php
$defaults = array(
'author_email' => '',
'author__in' => '',
'author__not_in' => '',
'include_unapproved' => '',
'fields' => '',
'ID' => '',
'comment__in' => '',
'comment__not_in' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_author__in' => '',
'post_author__not_in' => '',
'post_ID' => '', // ignored (use post_id instead)
'post_id' => 0,
'post__in' => '',
'post__not_in' => '',
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => 'all',
'type' => '',
'type__in' => '',
'type__not_in' => '',
'user_id' => '',
'search' => '',
'count' => false,
'meta_key' => '',
'meta_value' => '',
'meta_query' => '',
'date_query' => null, // See WP_Date_Query
);
?>
Make sure to edit the values as per your requirement. Using get_comments() you can get all comments and this way you can fetch first or any required comment id using loop.
Related
How can I get all pages that meet the criteria?
get_page_by_title()
The above only brings the first one. Is it possible for get_page_by_title to return an array of pages?
Here is the code to loop all pages with title:
<?php
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages( $args);
foreach ($pages as $page_data) {
$title = $page_data->post_title;
echo $title;
}
?>
You can change args to filter pages.
I can get users using $all_users = get_users(['meta_key' => 'first_name', 'meta_value' => 'john']); but I have a small problem now, as the meta value for feature I am trying to fetch is serialized.
I have copied a row from the table as shown below.
103 wp_capabilities a:1:{s:12:"cs_candidate";b:1;}
I can get the value and unserialize it, but not sure if there is a way to do this in WP.
UPDATE:
To add more clarification, I am referring to this page https://codex.wordpress.org/Function_Reference/get_users or the below function to be exact:
<?php $args = array(
'blog_id' => $GLOBALS['blog_id'],
'role' => '',
'role__in' => array(),
'role__not_in' => array(),
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'meta_query' => array(),
'date_query' => array(),
'include' => array(),
'exclude' => array(),
'orderby' => 'login',
'order' => 'ASC',
'offset' => '',
'search' => '',
'number' => '',
'count_total' => false,
'fields' => 'all',
'who' => ''
);
get_users( $args ); ?>
The 'meta_value' => '', part in the field is serialized a:1:{s:12:"cs_candidate";b:1;}
You can't search unserialize an search at the same time, I'm afraid. Searching in serialized data is always a bit of a hassle.
If you are certain of structure of the data you are searching, you could do something like:
$meta_args = [
'key' => 'wp_capabilities',
'value' => 'cs_candidate";b:1',
'compare' => 'LIKE'
];
$users = get_users(['meta_query' => $meta_args]);
It's anything but pretty, but should do the trick.
When building a WooCommerce site they have made it really easy to display categories and sub categories on the archive and category pages.
However does anyone know if it's possible to add a the list of categories/sub categories to the (content-single-product) page template?
I have a store that had only a handfull of products and we would like users to be able to quick select from the side menu rather than go back and forward between the archive page and the product pages.
Thanks to anyone who can help.
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
$woo_categories = get_categories( $prod_cat_args );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$return .= '' . $woo_cat_name . '';
}
$prod_cat_args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
you can also edit all fields try this
I am using Realto theme for wordpress I am wondering if I am on the correct path. I need to display properties from a city. So I decide to create a page template to add the location
This are the default arguments but I dont know how to filter the results by city
$args = array(
'numberposts' => '',
'posts_per_page' => $posts_per_page,
'offset' => 0,
'cat' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'property',
'post_mime_type' => '',
'post_parent' => '',
'paged' => $paged,
'post_status' => 'publish'
);
I tried adding
'locations' => 'MYCITY'
but it didnt work
This is an example of the search results when I search by city, so I am basing my arguments on this.
/?post_type=property&search_keyword=&locations=MYCITY&property_type=proyectos&beds=&baths=&status=&min-price=&max-price=
I assume that locations is a custom field? Maybe this works:
<?php
$args = array(
'post_type' => 'property',
'meta_key' => 'locations',
'meta_value' => 'MYCITY'
);
?>
Visit http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters for more details.
I want to be use some sliders in my wordpress theme. I want to select them in my theme option. I am using the code below.
<?php
$slider_select = get_option_tree( 'slider_select', '', 'true' );
?>
<?php get_template_part('$slider_select'); ?>
But, it is not working. I want the get_template_part code worked. Any suggestion?
replace
get_template_part('$slider_select');
with
get_template_part($slider_select);
You can use this method for create a slider in option tree.
create settings array like this.
array(
'id' => 'my_slider',
'label' => 'Images',
'desc' => '',
'std' => '',
'type' => 'list-item',
'section' => 'general',
'class' => '',
'choices' => array(),
'settings' => array(
array(
'id' => 'slider_image',
'label' => 'Image',
'desc' => '',
'std' => '',
'type' => 'upload',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_link',
'label' => 'Link to Post',
'desc' => 'Enter the posts url.',
'std' => '',
'type' => 'text',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_description',
'label' => 'Description',
'desc' => 'This text is used to add fancy captions in the slider.',
'std' => '',
'type' => 'textarea',
'class' => '',
'choices' => array()
)
)
)
Add into page using loop
$my_slider = ot_get_option( 'my_slider' );
foreach($my_slider as $slider){
echo '<img src="'.$slider["slider_image"].'">';
echo $slider["slider_link"];
echo $slider["slider_description"];
}